有没有办法让用户一次只检查一组 Checkbutton
小部件中的一个 Checkbutton
小部件?我可以想象一些强力的解决方案,但我正在寻找一些优雅的解决方案。
最佳答案
您可以将所有复选按钮绑定(bind)到具有不同onvalue的单个变量。
import tkinter
root = tk.Tk() #Creating the root window
var = tk.IntVar() #Creating a variable which will track the selected checkbutton
cb = [] #Empty list which is going to hold all the checkbutton
for i in range(5):
cb.append(tk.Checkbutton(root, onvalue = i, variable = var))
#Creating and adding checkbutton to list
cb[i].pack() #packing the checkbutton
root.mainloop() #running the main loop
出于演示目的,我在循环中创建了它们。即使您按顺序创建它们,也可以使用具有不同 onvalue 的相同变量名称。
关于python - 一次只允许用户选择一个复选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4092449/