一次只允许用户选择一个 Checkbutton? [英] Only allow user to select one Checkbutton at a time?

查看:36
本文介绍了一次只允许用户选择一个 Checkbutton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法只允许用户一次检查一组 Checkbutton 小部件中的一个 Checkbutton 小部件?我可以想象一些蛮力的解决方案,但我正在寻找一些优雅的方法.

Is there a way to only allow a user to check one Checkbutton widget at a time out of a set of Checkbutton widgets? I can imagine some brute-force solutions, but I'm looking for something elegant.

推荐答案

您可以将所有复选按钮绑定到具有不同 onvalue 的单个变量.

You can tie all the checkbutton to single variable with different 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 的相同变量名称.

I have created them in a loop for demonstration purpose. Even when you create them sequentially you can use the same variable name with different onvalue.

这篇关于一次只允许用户选择一个 Checkbutton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆