无论我对checkbutton做什么,它似乎都不会设置变量。 以下是涉及的代码部分:
class Window:
def __init__(self):
self.manualb = 0 #to set the default value to 0
def setscreen(self):
#screen and other buttons and stuff set here but thats all working fine
manual = tkr.Checkbutton(master=self.root, variable=self.manualb, command=self.setMan, onvalue=0, offvalue=1) #tried with and without onvalue/offvalue, made no difference
manual.grid(row=1, column=6)
def setMan(self):
print(self.manualb)
#does some other unrelated stuff
它只是继续打印0。我做错什么了吗?其他任何东西都不会影响手动操作。在
您正在寻找
IntVar()
IntVar()
有一个名为get()
的方法,它将保存您分配给它的小部件的值。在在这个特定的实例中,它将是1或0(打开或关闭)。 你可以这样使用它:
这是因为您需要使用tkinter的variable classes。在
如下所示:
本质上,
IntVar()
是一个“容器”(非常松散地说),它持有分配给它的小部件的值。在相关问题 更多 >
编程相关推荐