Python的標籤變化,我需要每一個我選擇一個不同的單選按鈕的時間來改變我的標籤背景顏色。在單選按鈕選擇
這裏是我的代碼的簡化例子。
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"
我可以請一些hep如何實現這一目標嗎? 謝謝。
什麼不工作是我不知道如何通過選擇單選按鈕來更改我的標籤顏色。我沒有收到特定的錯誤消息。我只是問我如何在每次選擇單選按鈕時更改我的標籤顏色。 if語句只是顯示了我想要使用我的單選按鈕的功能。 – GrandJoss
我的錯誤。我誤解了。添加了幾個標籤,刪除了我的近距離投票(取而代之),並回答了問題。你需要插入只有十餘個字符,使其準確地工作,你必須這樣:-) –