如何限制用户使用一个检查按钮检查 tkinter python [英] How to restrain user to one checkbuttons checked tkinter python

查看:31
本文介绍了如何限制用户使用一个检查按钮检查 tkinter python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有多个复选按钮的 GUI(复选按钮的数量是随机的,这取决于用户之前输入参数的文件).

I have created a GUI with several checkbuttons (the number of checkbuttons is random it depends on the file that the user put in parameter before).

所以我想知道是否可以限制用户一次检查一个检查按钮.

And so i would like to know if it's possible to restrain the user to one checkbuttons checked at once.

我知道我可以创建单选按钮,但事实是我对单选按钮有相同的变量和相同的值,当我使用单选按钮时,我可以选中两个按钮,但无法取消选中它们.这是我的代码:

I know i can create radiobuttons but the fact is i have same variable and same value for radiobuttons and when i use radiobuttons i can check both buttons and i can't uncheck them. Here is my code :

for element in self.listdiagram.dict_diagrams:
    diagramVar = IntVar()
    diagram = Radiobutton(self.window, text=element, variable=diagramVar, value=1)
    diagram.pack(side=BOTTOM, expand=1)

    self.diagramVars[self.listdiagram.dict_diagrams.get(element)] = diagramVar

self.validate = Button(self.window, text="Validate", command=self.validateCallBack, width=15, height=3)
self.validate.pack(side=BOTTOM, expand=1)

我有一个可与复选按钮配合使用的代码,但我不知道如何限制为一个复选按钮.

I have a code which works with checkbuttons but i don't know how to restrain to one checked.

感谢您的帮助!

这就是它的样子,值还可以,但两者都被选中,我无法取消选中它们,虽然单选按钮是当我选中另一个按钮时未选中.

Edit : This is what it looks like, value are ok but both are checked and i can't unchecked them i though radiobuttons was when i check one the other one is unchecked.

https://i.stack.imgur.com/yKJt8.png

图片 2:https://i.stack.imgur.com/iOnNV.png

推荐答案

问:我知道我可以创建单选按钮,但事实是我有相同的变量和单选按钮的值相同,当我使用单选按钮时,我可以选中两个按钮,我无法取消选中它们.

Q: I know i can create radiobuttons but the fact is i have same variable and same value for radiobuttons and when i use radiobuttons i can check both buttons and i can't uncheck them.

RadiobuttonsCheckbuttons 的工作方式略有不同.使用单选按钮,您可以创建组",其中只能选择一个组.您这样做的方法是让所有 Radiobuttons 使用相同的 IntVar() 作为它们的 variable= 字段,然后让每个 Radiobutton 具有不同的值.

Radiobuttons work a bit differently to Checkbuttons. With radio buttons, you can create "groups" where only one of the group can be selected. The way you do this is have all the Radiobuttons to use the same IntVar() as their variable= field, and then have each Radiobutton have a different value.

您可以使用 enumerate for loop 在循环中执行此操作,如下所示:

You can do this in a loop by using an enumerate for loop, like so:

diagramVars = {}
diagramVar = IntVar()

for i, element in enumerate(self.listdiagram.dict_diagrams):
    diagram = Radiobutton(self.window, text=element, variable=diagramVar, value=i)
    diagram.pack(side=BOTTOM, expand=1)

    self.diagramVars[self.listdiagram.dict_diagrams.get(element)] = diagramVar

self.validate = Button(self.window, text="Validate", command=self.validateCallBack, width=15, height=3)
self.validate.pack(side=BOTTOM, expand=1)

为此,您不需要字典,因为 IntVar 的值将是被选中的 RadioButton.

For this you wouldn't need a dictionary, since the value of the IntVar will be the RadioButton which is selected.

例如如果选择Radiobutton #1,则diagramVar.get() 将返回0,如果选择Radiobutton #2,则diagramVar.get() 将返回 1,依此类推.这是因为单选按钮组需要相同的 IntVar()

E.g. If Radiobutton #1 is selected, then the diagramVar.get() will return 0, If Radiobutton #2 is selected, then the diagramVar.get() will return 1, etc. This is because the groups of radio buttons need the same IntVar()

这篇关于如何限制用户使用一个检查按钮检查 tkinter python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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