Three lights operate with radio button
Can anyone help me to figure out the cause? The below code gives no error but no output:( On my root other icons appear (which is not included here) but the below portion of code not works?
from tkinter import *
root = Tk()
frame = Frame(root)
frame.pack()
color = StringVar()
radio_red = Radiobutton(frame, text="Red", bg="red", variable= color, value="R", command= on_RadioChange)
radio_red.grid(row=10, column=1)
radio_yellow = Radiobutton(frame, text="Yellow", bg="yellow", variable= color, value="Y", command= on_RadioChange)
radio_yellow.grid(row = 10, column = 2)
radio_green = Radiobutton(frame, text="Green", bg="green", variable= color, value="G", command= on_RadioChange)
radio_green.grid(row = 10, column = 3)
canvas = Canvas(root, width=450, height=300, bg="white")
canvas.pack()
oval_red = canvas.create_oval(10, 10, 110, 110, fill="white")
oval_yellow = canvas.create_oval(120, 10, 220, 110, fill="white")
oval_green = canvas.create_oval(230, 10, 330, 110, fill="white")
color.set('R')
canvas.itemconfig(oval_red, fill="red")
root.mainloop()
def on_RadioChange():
color = color.get()
if color == 'R':
canvas.itemconfig(oval_red, fill="red")
canvas.itemconfig(oval_yellow, fill="white")
canvas.itemconfig(oval_green, fill="white")
elif color == 'Y':
canvas.itemconfig(oval_red, fill="white")
canvas.itemconfig(oval_yellow, fill="yellow")
canvas.itemconfig(oval_green, fill="white")
elif color == 'G':
canvas.itemconfig(oval_red, fill="white")
canvas.itemconfig(oval_yellow, fill="white")
canvas.itemconfig(oval_green, fill="green")
You should probably understand how to use classes, but here is an example on how to use classes with your given code.
from tkinter import *
class TrafficLights:
def value(self):
root = Tk()
frame = Frame(root)
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(root, 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")
root.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")
a = TrafficLights()
a.value()
The main mistake was that you were not using self
and did not call the method value()
. So I fixed all those. So if you dont want to call value()
then you would use change def value(self)
with def __init__(self):
so that now you dont have to call a.value()
. I recommend you study more about OOP before proceeding to classes with python and tkinter.
UPDATE:
There is nothing much wrong, just rearrrange the code and change the color
inside function to colors
or something else, completed code would be:
from tkinter import *
root = Tk()
def on_RadioChange():
colors = color.get()
if colors == 'R':
canvas.itemconfig('oval_red', fill="red")
canvas.itemconfig('oval_yellow', fill="white")
canvas.itemconfig('oval_green', fill="white")
elif colors == 'Y':
canvas.itemconfig('oval_red', fill="white")
canvas.itemconfig('oval_yellow', fill="yellow")
canvas.itemconfig('oval_green', fill="white")
elif colors == 'G':
canvas.itemconfig('oval_red', fill="white")
canvas.itemconfig('oval_yellow', fill="white")
canvas.itemconfig('oval_green', fill="green")
frame = Frame(root)
frame.pack()
color = StringVar()
radio_red = Radiobutton(frame, text="Red", bg="red", variable=color, value="R", command=on_RadioChange)
radio_red.grid(row=10, column=1)
radio_yellow = Radiobutton(frame, text="Yellow", bg="yellow", variable=color, value="Y", command=on_RadioChange)
radio_yellow.grid(row = 10, column = 2)
radio_green = Radiobutton(frame, text="Green", bg="green", variable=color, value="G", command=on_RadioChange)
radio_green.grid(row = 10, column = 3)
canvas = Canvas(root, width=450, height=300, bg="white")
canvas.pack()
canvas.create_oval(10, 10, 110, 110, fill="white",tag='oval_red')
canvas.create_oval(120, 10, 220, 110, fill="white",tag='oval_yellow')
canvas.create_oval(230, 10, 330, 110, fill="white",tag='oval_green')
color.set('R')
canvas.itemconfig('oval_red', fill="red")
root.mainloop()
Why changing color
to colors
? When your using color
your actually referencing to color
with color
, which will raise an UnboundLocalError: local variable 'color' referenced before assignment
(since its inside of function?), so to get rid of this, just rename(only in this case). Is this the best way of doing this? I dont think so, there are some useless variables that you could get rid of here.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.