使用python创建交通灯

时间:2014-07-05 16:28:50

标签: python tkinter python-3.4

我正在尝试使用tkinter显示红绿灯。除了收音机按钮,我无法显示任何内容。我尝试过不同的调整来显示它但没有任何效果。

from tkinter import *


class TrafficLights:
    def __init__(self):
        window = Tk()
        window.title("Traffic Light")

        self.canvas = Canvas(window, width = 450, height = 300, bg = "white")
        self.canvas.pack()
        frame = Frame(window)
        frame.pack()
        self.v1 = IntVar()



        rbRed = Radiobutton(frame, text = "Red", bg = "red",
                variable = self.v1, value = 1,
                command = self.processRadiobutton)
        rbYellow = Radiobutton(frame, text = "Yellow", bg = "yellow",
                variable = self.v1, value = 2,
                command = self.processRadiobutton)               
        rbGreen = Radiobutton(frame, text = "Green", bg = "green",
                variable = self.v1, value = 3,              
                command = self.processRadiobutton)

        rbRed.grid(row = 10, column = 1)
        rbYellow.grid(row = 10, column = 2)
        rbGreen.grid(row = 10, column = 3)
        window.mainloop()


    def processRadiobutton(self):
            if self.v1.get() == 'R':
                self.lbl["fg"] = "red"
            elif self.v1.get() == 'Y':
                  self.lbl["fg"] = "yellow"
            elif self.v1.get() == 'G':
                  self.v1.lbl["fg"] = "Green"


    id = self.canvas.create_rectangle(200, 67, 265, 60)
    def displayRectangle(self):
          self.canvas.create_rectangle(200, 67, 265, 60, tags = "rect")
    def displayOval(self):
          self.canvas.create_oval(10, 10, 10, 10, fill='red')
    def displayOval(self):
          self.canvas.create_oval(20, 20, 20, 20, fill='yellow')
    def displayOval(self):
          self.canvas.create_oval(30, 30, 30, 30, fill='green')

TrafficLights()          

2 个答案:

答案 0 :(得分:2)

将功能分配给Radiobutton的示例,但您可以将功能分配给self.color

from Tkinter import *

class TrafficLights:

    def __init__(self):

        window = Tk()
        window.title("Traffic Light")

        frame = Frame(window)
        frame.pack()

        self.color = StringVar()

        radio_red = Radiobutton(frame, text="Red", bg="red", variable=self.color, value="R", command=self.on_RadioChange)
        radio_red.grid(row=10, column=1)

        radio_yellow = Radiobutton(frame, text="Yellow", bg="yellow", variable=self.color, value="Y", command=self.on_RadioChange)               
        radio_yellow.grid(row = 10, column = 2)

        radio_green = Radiobutton(frame, text="Green", bg="green", variable=self.color, value="G", command=self.on_RadioChange)
        radio_green.grid(row = 10, column = 3)

        self.canvas = Canvas(window, width=450, height=300, bg="white")
        self.canvas.pack()

        self.oval_red = self.canvas.create_oval(10, 10, 110, 110, fill="white")
        self.oval_yellow = self.canvas.create_oval(120, 10, 220, 110, 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="red")

        window.mainloop()

    def on_RadioChange(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()

enter image description here

答案 1 :(得分:0)

from tkinter import *

class TrafficLights:

    def __init__(self):

        window = Tk()
        window.title("Traffic Light")


        frame = Frame(window)
        frame.pack()

        self.color = StringVar()

        radio_red = Radiobutton(frame, text="Red", bg="red", variable=self.color, value="R", command=self.on_RadioChange)
        radio_red.grid(row=10, column=1)

        radio_yellow = Radiobutton(frame, text="Yellow", bg="yellow", variable=self.color, value="Y", command=self.on_RadioChange)               
        radio_yellow.grid(row = 10, column = 2)

        radio_green = Radiobutton(frame, text="Green", bg="green", variable=self.color, value="G", command=self.on_RadioChange)
        radio_green.grid(row = 10, column = 3)

        self.canvas = Canvas(window, width=450, height=300, bg="white")
        self.canvas.pack()

        self.oval_red = self.canvas.create_oval(10, 10, 110, 110, fill="white")
        self.oval_yellow = self.canvas.create_oval(120, 10, 220, 110, 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="red")

        window.mainloop()

    def on_RadioChange(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")

            self.canvas.create_rectangle(3, 5, 350, 130)
TrafficLights()