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

时间:2018-05-17 11:35:58

标签: python tkinter

我有一个带有成员self.checkbutton的类(使用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_entry_and_execute”函数被调用,它甚至会改变一些背景颜色。但是,检查按钮仍未选中(即没有“V”符号的空字段)。 Weirly,当我添加命令

        self.checkbutton.set(value=1)

,代码抱怨: AttributeError:Checkbutton实例没有属性'set' 但现在检查按钮 得到检查!

我假设因为这个错误,python将checkbutton置于正确的状态(= 1),即使“set”函数不存在。我的问题是:我怎样才能正确地让python将“V”放在checkbutton框中? (即,类似“重绘”功能)。

1 个答案:

答案 0 :(得分:0)

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