Python中的单选按钮(TKinter)
我正在做一些家庭作业,我被困在这个练习上了。任务是在3列中创建12个标签,每列下有一个单选按钮。选择单选按钮时,其上方行的颜色需要更改。当取消选择单选按钮时,它们会变回原来的颜色。程序启动时,不得选择任何单选按钮 我有两个问题Python中的单选按钮(TKinter),python,python-2.7,tkinter,radio-button,Python,Python 2.7,Tkinter,Radio Button,我正在做一些家庭作业,我被困在这个练习上了。任务是在3列中创建12个标签,每列下有一个单选按钮。选择单选按钮时,其上方行的颜色需要更改。当取消选择单选按钮时,它们会变回原来的颜色。程序启动时,不得选择任何单选按钮 我有两个问题 我不知道如何在所有单选按钮都未选中的情况下启动程序。目前,一次选择两个 当程序加载时,改变方框颜色的功能似乎会运行。它们不会变回原来的颜色 这是我的密码: # Import the Tkinter functions from Tkinter import * # Cr
# 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
我想不出如何用所有的收音机开始这个节目
未选择的按钮。目前,一次选择两个
使用值与任何单选按钮的值都不匹配的变量(例如0)
改变盒子颜色的功能似乎在
程序加载。它们不会变回原来的颜色
传递函数本身,而不是函数调用的返回值。换句话说,删除()
command=change\u third\u set\u color()
->command=change\u third\u set\u color
顺便说一句,
place
应该单独调用,否则Radio\u 1
,Radio\u 2
,Radio\u 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) # <---
radio\u var=IntVar(值=0)
单选按钮1=单选按钮(单选按钮窗口,
text=“First”,
变量=radio_var,#
我想不出如何用所有的收音机开始这个节目
未选择按钮。当前,同时选择两个按钮
使用值与任何单选按钮的值都不匹配的变量(例如0)
改变盒子颜色的功能似乎在
程序加载。它们不会变回原来的颜色
传递函数本身,而不是函数调用的返回值。换句话说,删除()
command=change\u third\u set\u color()
->command=change\u third\u set\u color
顺便说一句,place
应该单独调用,否则Radio\u 1
,Radio\u 2
,Radio\u 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) # <---
radio\u var=IntVar(值=0)
单选按钮1=单选按钮(单选按钮窗口,
text=“First”,
变量=radio_var,#
我不知道如何在所有单选按钮都未选中的情况下启动程序。目前,两个单选按钮同时被选中。
对于这个问题,首先需要使用“var”将它们作为组。您已经给出了值,但也应该给出var以将它们作为组,并获取与它们关联的值
var=IntVar()
R1=单选按钮(根,text=“选项1”,变量=var,值=1,
命令=sel)
R2=单选按钮(根,text=“选项1”,变量=var,值=1,
命令=sel)
并使用命令实现获取值,如
def sel():
selection=“您选择了选项”+str(var.get())
或者你可以在你的箱子里找到颜色代码
2更改方框颜色的功能似乎在程序加载时运行。它们不会更改回原始颜色
对于我上面指定的这个问题,您可以命令方法设置新的颜色,并记住在更改颜色之前获取标签的颜色,并将其保存在类或全局变量中的本地。然后从新代码旧的/原始的颜色还原回来会很有帮助
我不知道如何在所有单选按钮都未选中的情况下启动程序。目前,两个单选按钮同时被选中。
对于这个问题,首先需要使用“var”将它们作为组。您已经给出了值,但也应该给出var以将它们作为组,并获取与它们关联的值
var=IntVar()
R1=单选按钮(根,text=“选项1”,变量=var,值=1,
命令=sel)
R2=单选按钮(根,text=“选项1”,变量=var,值=1,
命令=sel)
并使用命令实现获取值,如
def sel():
selection=“您选择了选项”+str(var.get())
或者你可以在你的箱子里找到颜色代码
2更改方框颜色的功能似乎在程序加载时运行。它们不会更改回原始颜色
对于我上面指定的这个问题,您可以命令方法设置新的颜色,并记住在更改颜色之前获取标签的颜色,并将其保存在类或全局变量中的本地。然后从新代码旧的/原始的颜色还原回来会很有帮助