我在我的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)更多推荐
发布评论