Python中的单选按钮(TKinter)

克里斯托弗·利特伍德(Christopher Littlewood)

我在做一些家庭作业时很忙,但我坚持做这项运动。任务是在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

虚假的

我无法弄清楚如何在未选中所有单选按钮的情况下启动程序。当前,一次选择两个。

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

程序加载时,更改框颜色的功能似乎正在运行。它们不会变回原始颜色。

传递函数本身,而不返回函数调用的值。换句话说,删除()

command=change_third_set_colour() -> command=change_third_set_colour


BTW,place应分别叫,否则Radio_1Radio_2Radio_3成为Noneplace方法返回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_colourchange_second_set_colourchange_third_set_colour应修改重置其他标签的颜色。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python中的单选按钮(TKinter)

来自分类Dev

Python Tkinter中的单选按钮值

来自分类Dev

单选按钮变量未更改Python(TKinter)中的值

来自分类Dev

如何从tkinter中的单选按钮获取值?

来自分类Dev

在Python 3中使用tkinter更改单选按钮的背景色

来自分类Dev

在Python 3中使用tkinter更改单选按钮的背景色

来自分类Dev

如何使用 Tkinter 在 python 中的不同时间显示不同的单选按钮组?

来自分类Dev

Python:使用Tkinter单选按钮从GUI添加/删除字段

来自分类Dev

为什么Python Tkinter单选按钮向左对齐?

来自分类Dev

Python 2.7 Tkinter-确定已选择哪个单选按钮

来自分类Dev

基于使用Python Tkinter的多个单选按钮选择的显示列表

来自分类Dev

为什么Python Tkinter单选按钮向左对齐?

来自分类Dev

Python:使用Tkinter单选按钮从GUI添加/删除字段

来自分类Dev

Python 2.7 Tkinter-确定已选择哪个单选按钮

来自分类Dev

Python-悬停时Tkinter单选按钮突出显示

来自分类Dev

如何在Tkinter中获取单选按钮输出?

来自分类Dev

使用.grid时无法在Tkinter中调用单选按钮

来自分类Dev

Tkinter 单选按钮在类中仅返回原始 set() 值

来自分类Dev

在maya / python中传递单选按钮的名称

来自分类Dev

Tkinter单选按钮奇怪的外观

来自分类Dev

HTML中的单选按钮

来自分类Dev

Netbeans中的单选按钮

来自分类Dev

Pygame中的单选按钮?

来自分类Dev

django中的单选按钮

来自分类Dev

电梯中的单选按钮?

来自分类Dev

DataTemplate中的单选按钮

来自分类Dev

Pygame中的单选按钮?

来自分类Dev

formArray中的单选按钮

来自分类Dev

集合中的单选按钮