代码之家  ›  专栏  ›  技术社区  ›  PyPunk

如何在TKInter中创建3个相互重叠的椭圆

  •  0
  • PyPunk  · 技术社区  · 8 年前

    我需要创建一个刹车灯模拟,但我一辈子都无法使这些圆相互重叠。网格从左上角开始吗?X轴和Y轴的行为似乎不像我预期的那样。

        from tkinter import *
    
        class TrafficLights:
    
        def __init__(self):
    
            window = Tk()
            window.title("Traffic Light")
    
            frame = Frame(window)
            frame.pack()
    
            self.color = StringVar()
    
            #Create Button options and corresponding colors
    
            #Red
            radio_red = Radiobutton(frame, text="Red", bg="red", variable=self.color, value="R", command=self.checkSelect)
            radio_red.grid(row=10, column=1)
    
            #Yellow
            radio_yellow = Radiobutton(frame, text="Yellow", bg="yellow", variable=self.color, value="Y", command=self.checkSelect)               
            radio_yellow.grid(row = 20, column = 1)
    
            #Green
            radio_green = Radiobutton(frame, text="Green", bg="green", variable=self.color, value="G", command=self.checkSelect)
            radio_green.grid(row = 30, column = 1)
    
            #Create canvas window and rectange for light
            self.canvas = Canvas(window, width=300, height=400, bg="white")
            self.canvas.create_rectangle(10,10,110,400)
            self.canvas.pack()
    
    
    
    
    
            #I cant seem to get the yellow and green where I need it.
            #Where is the center of the grid?
    
            self.oval_red = self.canvas.create_oval(10, 10, 110, 110, fill="white")
    
            self.oval_yellow = self.canvas.create_oval(100, 110, 310, 400, fill="white")
    
            self.oval_green = self.canvas.create_oval(230, -10, 330, 110, fill="white")
    
    
            self.color.set("R")
            self.canvas.itemconfig(self.oval_red, fill="white")
    
            window.mainloop()
    
        def checkSelect(self):
            color = self.color.get()
    
    
    
            if color == "R":
                self.canvas.itemconfig(self.oval_red, fill="red")
                self.canvas.itemconfig(self.oval_yellow, fill="white")
                self.canvas.itemconfig(self.oval_green, fill="white")
            elif color == "Y":
                self.canvas.itemconfig(self.oval_red, fill="white")
                self.canvas.itemconfig(self.oval_yellow, fill="yellow")
                self.canvas.itemconfig(self.oval_green, fill="white")
            elif color == "G":
                self.canvas.itemconfig(self.oval_red, fill="white")
                self.canvas.itemconfig(self.oval_yellow, fill="white")
                self.canvas.itemconfig(self.oval_green, fill="green")
    
    
    TrafficLights()
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   PyPunk    8 年前

    我能够确定TKinter使用了不同的坐标系。它使用传统坐标系的右下侧。有一次我发现我能找到正确的坐标。