无论状态如何,Checkbutton都会自行检查(Checkbutton checks itself regardless of state)

编程入门 行业动态 更新时间:2024-10-28 04:15:23
无论状态如何,Checkbutton都会自行检查(Checkbutton checks itself regardless of state)

我在我的tkinter窗口中有一个checkbutton,但是它工作正常,但是无论我做什么,它都会以灰色显示并开始检查。 我必须点击一次才能删除灰色,然后再次取消选中它。 我不知道它为什么这样做,除了它只在使用StringVar时发生,而不是例如IntVar。 我只是希望它像任何其他常规复选框一样不受检查地启动。 这是我的代码:

self.symptom1Var = StringVar() self.symptomCheck1 = Checkbutton(self, text="Symptom", variable = self.symptom1Var) self.symptomCheck1.grid(row=2, column=0)

I have a checkbutton in my tkinter window that is otherwise working fine, but it starts as greyed out and checked no matter what I do. I have to click it once to remove the grey, then again to uncheck it. I have no idea why it's doing this, other than that it only happens when using StringVar, as opposed to IntVar for example. I just want it to start unchecked like any other regular checkbox would do. Here's my code:

self.symptom1Var = StringVar() self.symptomCheck1 = Checkbutton(self, text="Symptom", variable = self.symptom1Var) self.symptomCheck1.grid(row=2, column=0)

最满意答案

唯一的问题是你没有给checkbutton一个默认值,所以它开始处于一个不确定的状态。

StringVar的初始值是一个空字符串,checkbutton的默认“on”值是1 ,checkbutton的默认“off”值是0 。 由于您尚未将其设置为1或0 ,因此它处于不确定状态(既未选中也未取消选中)。

解决方案是通过设置相关变量来确保将其设置为已选中状态或未选中状态:

self.symptom1Var.set(1)

The only problem is that you're not giving the checkbutton a default value so it starts out in an indeterminate state.

The initial value of a StringVar is an empty string, the default "on" value of a checkbutton is 1 and the default "off" value of a checkbutton is 0. Since you haven't set it to be either 1 or 0, it's in the indeterminate state (neither checked nor unchecked).

The solution is to make sure it's set to either the checked or unchecked state by setting the associated variable:

self.symptom1Var.set(1)

更多推荐

本文发布于:2023-07-26 18:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1279315.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:都会   状态   state   Checkbutton   checks

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!