设置为1后,python checkbutton外观不会更新

2024-05-13 23:31:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个班有一个成员自检按钮(使用TkInter CheckButton),在init函数中创建,使用:

self.checkbutton = Checkbutton(self.frame, variable=self.value, command=self.get_entry_and_execute, onvalue="1", offvalue="0")

现在,我想在代码中设置它,而不是单击框架中的checkbutton。因此,在我的代码中的某个地方,我调用setvalue(“1”)调用这个函数:

def setvalue(self, value):
    if (value == "1"):
        print "SELECTING CHECKBUTTON"
        self.checkbutton.select()
        #   self.checkbutton.set(value=1)

现在,当我这样做的时候,实际上,我可以看到self.get\u entry\u和\u execute函数被调用,它甚至改变了一些背景颜色。但是,checkbutton保持未选中状态(即没有“V”符号的空字段)。 奇怪,当我加上命令

        self.checkbutton.set(value=1)

,代码抱怨:AttributeError:Checkbutton实例没有属性“set” 但是现在checkbutton确实被选中了!你知道吗

我假设由于这个错误,python将checkbutton置于正确的状态(=1),即使“set”函数不存在。我的问题是:如何让python正确地将“V”放在复选框中?(例如,类似于“重画”函数)。你知道吗


Tags: 函数代码selfexecutegetvaluetkinter状态
1条回答
网友
1楼 · 发布于 2024-05-13 23:31:47

根据您的代码,self.valuevariable class的一个实例,所以您需要做的就是用self.value.set(value=1)替换self.checkbutton.set(value=1)

相关问题 更多 >