每次选择不同的单选按钮时,我都需要更改标签背景颜色。
以下是我的代码的简化示例。
Main_Window = Tk()
Main_Window.title("Main Window")
Main_Window.geometry("500x500+25+25")
Label_1 = Label(Main_Window, text="Label 1", width=8, bg=Label_1_Color, fg="black", bd=2, relief=RIDGE, padx=2, pady=2)
Label_1.pack(side=TOP, anchor=W)
Label_2 = Label(Main_Window, text="Label 2", width=8, bg=Label_2_Color, fg="black", bd=2, relief=RIDGE, padx=2, pady=2)
Label_2.pack(side=TOP, anchor=W)
Label_3 = Label(Main_Window, text="Label 3", width=8, bg=Label_3_Color, fg="black", bd=2, relief=RIDGE, padx=2, pady=2)
Label_3.pack(side=TOP, anchor=W)
Label_4 = Label(Main_Window, text="Label 4", width=8, bg=Label_4_Color, fg="black", bd=2, relief=RIDGE, padx=2, pady=2)
Label_4.pack(side=TOP, anchor=W)
Radio_Group = IntVar()
Radio_1 = Radiobutton(Main_Window, text="Option 1", bg="white", variable=Radio_Group, value=1)
Radio_1.pack(side=BOTTOM, anchor=W)
Radio_1.invoke()
Radio_2 = Radiobutton(Main_Window, text="Option 2", bg="white", variable=Radio_Group, value=2)
Radio_2.pack(side=BOTTOM, anchor=W)
Radio_3 = Radiobutton(Main_Window, text="Option 3", bg="white", variable=Radio_Group, value=3)
Radio_3.pack(side=BOTTOM, anchor=W)
Radio_4 = Radiobutton(Main_Window, text="Option 4", bg="white", variable=Radio_Group, value=4)
Radio_4.pack(side=BOTTOM, anchor=W)
Radio_5 = Radiobutton(Main_Window, text="Option 5", bg="white", variable=Radio_Group, value=5)
Radio_5.pack(side=BOTTOM, anchor=W)
Main_Window.mainloop()
我希望他们在选择不同的单选按钮时多次更改这些条件。
if Radio_Group.get() == 1:
Label_1_Color = "white"
Label_2_Color = "white"
Label_3_Color = "white"
Label_4_Color = "white"
elif Radio_Group.get() == 2:
Label_1_Color = "red"
Label_2_Color = "white"
Label_3_Color = "white"
Label_4_Color = "white"
elif Radio_Group.get() == 3:
Label_1_Color = "white"
Label_2_Color = "red"
Label_3_Color = "white"
Label_4_Color = "white"
elif Radio_Group.get() == 4:
Label_1_Color = "white"
Label_2_Color = "white"
Label_3_Color = "red"
Label_4_Color = "white"
else:
Label_1_Color = "white"
Label_2_Color = "white"
Label_3_Color = "white"
Label_4_Color = "red"
我能否详细了解如何实现这一目标? 谢谢。
答案 0 :(得分:1)
根据Tkinter docs at Effbot,您可以为每个Radiobutton
添加回调。使用command
关键字传递一个可调用的函数,该关键字可以执行任何操作。该选项列在config
下,但也可在__init__
中找到。在这种情况下,您可以将所需的代码完全按原样转换为函数,并将其用作命令:
Main_Window = Tk()
Main_Window.title("Main Window")
Main_Window.geometry("500x500+25+25")
Label_1 = Label(Main_Window, text="Label 1", width=8, bg=Label_1_Color, fg="black", bd=2, relief=RIDGE, padx=2, pady=2)
Label_1.pack(side=TOP, anchor=W)
...
Radio_Group = IntVar()
def setColor():
if Radio_Group.get() == 1:
Label_1.configure(background="white")
Label_2.configure(background="white")
Label_3.configure(background="white")
Label_4.configure(background="white")
...
Radio_1 = Radiobutton(Main_Window, text="Option 1", bg="white", variable=Radio_Group, value=1, command=setColor)
Radio_1.pack(side=BOTTOM, anchor=W)
Radio_1.invoke()
...
...
表示您在问题中已有的代码。
Effbot article on callback commands有一整节涉及使用相同回调的多个按钮的确切情况。在您的情况下,您已使用Radio_Group
来解决问题。
为了使您的代码看起来更漂亮并删除巨大的嵌套if语句,您可以使用列表,元组和循环。请注意,索引已更改为0。例如:
Main_Window = Tk()
Main_Window.title("Main Window")
Main_Window.geometry("500x500+25+25")
# A dictionary keyed by radio button containing sequences of colors for each label
labels = []
#labelCount = 4
for i in range(labelCount):
label = Label(Main_Window, text="Label {}".format(i + 1), width=8, bg='white', fg="black", bd=2, relief=RIDGE, padx=2, pady=2))
label.pack(side=TOP, anchor=W)
labels.append(label)
Radio_Group = IntVar()
def setColor():
radioIndex = Radio_Group.get()
for labelIndex in range(labelCount):
if labelIndex == radioIndex - 1:
color = 'red'
else:
color = 'white'
labels[labelIndex].configure(background=color)
radios = []
#radioCount = 5
for i in range(radioCount):
radio = Radiobutton(Main_Window, text="Option {}".format(i + 1), bg="white", variable=Radio_Group, value=i, command=setColor)
radio.pack(side=BOTTOM, anchor=W)
if i == 0:
radio.invoke()
radios.append(radio)