user3808080 user3808080 - 5 months ago 12
Python Question

creating a traffic light using python

I am trying to display a traffic light using the tkinter. I am unable to get anything to display except for the radio buttons. I have tried different adjustments to get it to display but nothing is working.

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

Answer

Example with function assigned to Radiobutton but you could assign function to 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