【问题标题】:Radiobutton variable not changing value in Python (TKinter)单选按钮变量在 Python (TKinter) 中不改变值
【发布时间】:2016-09-01 04:32:28
【问题描述】:

我正在为大学开展一项活动,但我坚持解决另一个问题。我在网上搜索过,但它没有告诉我我做错了什么。我们必须创建 12 个框,设置为 3 列,每列 4 列,每列下方有一个单选按钮。选择单选按钮时,框的颜色会发生变化。当单选按钮未选中时,它们将返回其原始颜色。

在添加我的 if 语句以确定单选按钮的变量值是什么之后,什么也没有发生。

这是我的代码:

# Import the Tkinter functions
from Tkinter import *

# Create a window
the_window = Tk()
the_window.geometry('460x200')

# Give the window a title
the_window.title('Show Columns')

#Declare Radio Variable
radio_var = IntVar(value=0)

#Change first set colour
def change_first_set_colour():

    #Determine colour to display
    if radio_var == 1:
        label1.configure(bg="blue")
        label2.configure(bg="blue")
        label3.configure(bg="blue")
        label4.configure(bg="blue")
    else:
        label1.configure(bg="grey")
        label2.configure(bg="grey")
        label3.configure(bg="grey")
        label4.configure(bg="grey")

#Change first set colour
def change_second_set_colour():

    #Determine colour to display
    if radio_var == 2:
        label1.configure(bg="blue")
        label2.configure(bg="blue")
        label3.configure(bg="blue")
        label4.configure(bg="blue")
    else:
        label1.configure(bg="grey")
        label2.configure(bg="grey")
        label3.configure(bg="grey")
        label4.configure(bg="grey")

#Change first set colour
def change_third_set_colour():

    #Determine colour to display
    if radio_var == 3:
        label1.configure(bg="blue")
        label2.configure(bg="blue")
        label3.configure(bg="blue")
        label4.configure(bg="blue")
    else:
        label1.configure(bg="grey")
        label2.configure(bg="grey")
        label3.configure(bg="grey")
        label4.configure(bg="grey")

#Create label1
label1 = Label(the_window, bg="grey", fg="black", width=20, height=2)
label1.place(x=5, y=5)

#Create label2
label2 = Label(the_window, bg="grey", fg="black", width=20, height=2)
label2.place(x=5, y=45)

#Create label3
label3 = Label(the_window, bg="grey", fg="black", width=20, height=2)
label3.place(x=5, y=85)

#Create label4
label4 = Label(the_window, bg="grey", fg="black", width=20, height=2)
label4.place(x=5, y=125)

#Create Radio Button 1
Radio_1 = Radiobutton(the_window,
                      text="First",
                      variable=radio_var,
                      command=change_first_set_colour,
                      value=1)
Radio_1.place(x=50, y=165)

#Create label5
label5 = Label(the_window, bg="grey", fg="black", width=20, height=2)
label5.place(x=155, y=5)

#Create label6
label6 = Label(the_window, bg="grey", fg="black", width=20, height=2)
label6.place(x=155, y=45)

#Create label7
label7 = Label(the_window, bg="grey", fg="black", width=20, height=2)
label7.place(x=155, y=85)

#Create label8
label8 = Label(the_window, bg="grey", fg="black", width=20, height=2)
label8.place(x=155, y=125)

#Create Radio Button 2
Radio_2 = Radiobutton(the_window,
                      text="Second",
                      variable=radio_var,
                      command=change_second_set_colour,
                      value=2)
Radio_2.place(x=180, y=165)

#Create label9
label9 = Label(the_window, bg="grey", fg="black", width=20, height=2)
label9.place(x=305, y=5)

#Create label10
label10 = Label(the_window, bg="grey", fg="black", width=20, height=2)
label10.place(x=305, y=45)

#Create label11
label11 = Label(the_window, bg="grey", fg="black", width=20, height=2)
label11.place(x=305, y=85)

#Create label12
label12 = Label(the_window, bg="grey", fg="black", width=20, height=2)
label12.place(x=305, y=125)

Radio_3 = Radiobutton(the_window,
                      text="Third",
                      variable=radio_var,
                      command=change_third_set_colour,
                      value=3)
Radio_3.place(x=345, y=165)

#----------------------------------------------------------------

# Start the event loop to react to user inputs
the_window.mainloop()

任何帮助将不胜感激! 谢谢

【问题讨论】:

    标签: python tkinter radio-button gui-design


    【解决方案1】:

    radio_var 是一个对象。您需要使用 radio_var.get() 才能正确检索值。

    【讨论】:

      猜你喜欢
      • 2015-06-15
      • 1970-01-01
      • 2015-09-11
      • 1970-01-01
      • 2014-09-14
      • 1970-01-01
      • 2014-03-16
      • 2020-01-20
      • 2021-04-03
      相关资源
      最近更新 更多