1 回答
TA贡献1946条经验 获得超3个赞
您可能应该了解如何使用类,但这里有一个有关如何在给定代码中使用类的示例。
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(),那么您可以将 Changedef value(self)与 def 一起使用__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实际引用colorwith时color,这会引发一个UnboundLocalError: local variable 'color' referenced before assignment(因为它在函数内部?),因此要摆脱它,只需重命名(仅在这种情况下)。这是最好的方法吗?我不这么认为,您可以在这里删除一些无用的变量。
添加回答
举报