三个灯通过单选按钮操作
谁能帮我找出原因吗?下面的代码没有给出错误,但没有输出:( 在我的根目录上出现其他图标(此处不包括),但下面的代码部分不起作用?
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")
智慧大石
相关分类