【问题标题】:Radiobuttons in Python (TKinter)Python 中的单选按钮 (TKinter)
【发布时间】:2016-09-01 02:45:17
【问题描述】:

我正在做一些功课,但我坚持做这个练习。任务是在 3 列中创建 12 个标签,每列下都有一个单选按钮。当一个单选按钮被选中时,它上面的行的颜色需要改变。当取消选择单选按钮时,它们会变回原来的颜色。程序启动时,必须选择任何单选按钮。

我有两个问题。

  1. 我不知道如何在未选中所有单选按钮的情况下启动程序。目前,一次选择两个。
  2. 更改框颜色的函数似乎在程序加载时运行。它们不会变回原来的颜色。

这是我的代码:

# 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')

#Change first set colour
def change_first_set_colour():
    label1.configure(bg="blue")
    label2.configure(bg="blue")
    label3.configure(bg="blue")
    label4.configure(bg="blue")

#Change first set colour
def change_second_set_colour():
    label5.configure(bg="blue")
    label6.configure(bg="blue")
    label7.configure(bg="blue")
    label8.configure(bg="blue")

#Change first set colour
def change_third_set_colour():
    label9.configure(bg="blue")
    label10.configure(bg="blue")
    label11.configure(bg="blue")
    label12.configure(bg="blue")

#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",
                      command=change_first_set_colour(),
                      value=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",
                      command=change_second_set_colour(),
                      value=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",
                      command=change_third_set_colour(),
                      value=3).place(x=345, y=165)

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

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

PS:我的大学还在使用 Python 2.7

【问题讨论】:

    标签: python python-2.7 tkinter radio-button


    【解决方案1】:
    1. 我不知道如何在未选中所有单选按钮的情况下启动程序。目前,一次选择两个。
      对于这个问题,首先您需要使用 'var' 将它们作为组。您已经给出了值,但您也应该给出 var 以使它们成为组并获取与它们关联的值
      var = IntVar()
      R1 = Radiobutton(root, text="Option 1", variable=var, value=1, command=sel) R2 = Radiobutton(root, text="Option 1", variable=var, value=1, command=sel)

      并使用类似的命令实现来获取值 def sel(): selection = "You selected the option " + str(var.get())

      或者您可以获取您的案例中的颜色代码

    2 更改框颜色的函数似乎在程序加载时运行。它们不会变回原来的颜色
    对于我上面指定的这个问题,您可以使用命令方法设置新颜色,并记住在更改颜色之前获取标签的颜色并本地保存在类或全局变量中。那么从新代码中恢复旧/原始颜色会很有帮助

    【讨论】:

      【解决方案2】:

      我不知道如何使用所有收音机启动程序 按钮未选中。目前,一次选择两个。

      使用值与任何单选按钮的值都不匹配的变量(例如 0)。

      更改框颜色的函数似乎在 程序加载。它们不会变回原来的颜色。

      传递函数本身,而不是函数调用的返回值。也就是说,去掉()

      command=change_third_set_colour() -> command=change_third_set_colour


      顺便说一句,place 应该单独调用,否则 Radio_1、Radio_2、Radio_3 将变为 None,因为 place 方法返回 None。

      radio_var = IntVar(value=0)
      
      Radio_1 = Radiobutton(the_window,
                            text="First",
                            variable=radio_var,  # <---
                            command=change_first_set_colour,  # <---
                            value=1)
      Radio_1.place(x=50, y=165)  # <---
      
      ...
      
      Radio_2 = Radiobutton(the_window,
                            text="Second",
                            variable=radio_var,  # <---
                            command=change_second_set_colour,  # <---
                            value=2)
      Radio_2.place(x=180, y=165)  # <---
      
      ...
      
      Radio_3 = Radiobutton(the_window,
                            text="Third",
                            variable=radio_var,  # <---
                            command=change_third_set_colour,  # <---
                            value=3)
      Radio_3.place(x=345, y=165)  # <---
      

      change_first_set_colour,change_second_set_colour,change_third_set_colour应该修改为重置其他标签的颜色。

      【讨论】:

        猜你喜欢
        • 2016-09-11
        • 1970-01-01
        • 2016-06-10
        • 1970-01-01
        • 2014-03-16
        • 2021-06-09
        • 1970-01-01
        • 2013-03-20
        • 2018-09-16
        相关资源
        最近更新 更多