三个灯通过单选按钮操作
有人可以帮助我找出原因吗?下面的代码没有错误,但是没有输出:(在我的根目录下,出现了其他图标(此处未包括),但是下面的代码不起作用?
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")
答案 0 :(得分:0)
您可能应该了解如何使用类,但这是有关如何在给定代码中使用类的示例。
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()
主要错误是您没有使用self
并且没有调用方法value()
。因此,我修复了所有这些问题。因此,如果您不想调用value()
,则可以将def def value(self)
与更改__init__(self):
一起使用,这样现在就不必调用a.value()
。我建议您在继续使用python和tkinter进行学习之前,先学习有关OOP的更多信息。
更新:
没什么大不了的,只是重新排列代码并将color
内部函数更改为colors
或其他内容,完成的代码将是:
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()
为什么将color
更改为colors
?当您使用color
实际使用color
来引用color
时,这将引发UnboundLocalError: local variable 'color' referenced before assignment
(因为它在函数内部?),因此要摆脱它,只需重命名(仅在这种情况下)。这是最好的方法吗?我不这么认为,这里有一些无用的变量可以消除。