Python Checkbutton赢了';不能设置为可变值
我创建了一个简单的Python Tkinter gui,但我无法使用正确的值使Checkbutton初始化,无论我做什么,这两个都始终处于未选中状态,而代码中的两个显示:Python Checkbutton赢了';不能设置为可变值,python,tkinter,python-2.x,Python,Tkinter,Python 2.x,我创建了一个简单的Python Tkinter gui,但我无法使用正确的值使Checkbutton初始化,无论我做什么,这两个都始终处于未选中状态,而代码中的两个显示: ('dryRunVar', 0) ('useGenreSubFolderVar', 1) 我也尝试过BooleanVar,但没能成功 奇怪的是,如果我将CheckButton()的instanceation更改为ttk.CheckButton(),那么两个按钮都处于“灰色”状态 即使将对配置值的调用更改为常量0和1也不会改变
('dryRunVar', 0)
('useGenreSubFolderVar', 1)
我也尝试过BooleanVar,但没能成功
奇怪的是,如果我将CheckButton()的instanceation更改为ttk.CheckButton(),那么两个按钮都处于“灰色”状态
即使将对配置值的调用更改为常量0和1也不会改变任何内容,按钮保持未选中状态
还尝试在Tk实例化和mainloop周围进行一些调整,但没有成功
#!/usr/lib/python2.7/
# -*- coding: utf-8 -*-
from Tkinter import *
import conf,ttk
class GUI():
def __init__(self,window, configuration) :
self.configuration = configuration
self.window = window
self.draw()
def draw(self) :
self.root = Frame(self.window,padx=15,pady=15,width=800,height=200)
self.root.grid(column=0,row=0)
self.drawParametersFrame()
def drawParametersFrame(self) :
#Parameters frame
self.parametersFrame = LabelFrame(self.root,text="Sorting Parameters",padx=15,pady=15)
self.parametersFrame.grid(column=0,row=2,sticky="EW")
dryRunVar = IntVar()
dryRunVar.set(self.configuration['dryRun'])
print("dryRunVar",dryRunVar.get())
dryRunCheckButton = Checkbutton(self.parametersFrame,text="Dry Run", variable=dryRunVar, onvalue=1, offvalue = 0)
dryRunCheckButton.grid(column=0,row=0,sticky="W")
useGenreSubFolderVar = IntVar()
useGenreSubFolderVar.set(self.configuration['genreSubFolders'])
print("useGenreSubFolderVar",useGenreSubFolderVar.get())
useGenreSubFolderCheckButton = Checkbutton(self.parametersFrame,text="Use genre subfolders", variable=useGenreSubFolderVar, onvalue=1, offvalue = 0)
useGenreSubFolderCheckButton.grid(column=2,row=0,sticky="W")
if __name__ == "__main__":
configuration = conf.loadConf(r"/home/thomas/code/perso/python/conf.conf")
window = Tk()
gui = GUI(window,configuration)
window.mainloop()
将您的
IntVar
改为类的属性
def drawParametersFrame(self) :
...
self.dryRunVar = IntVar()
self.dryRunVar.set(1)
dryRunCheckButton = Checkbutton(self.parametersFrame,text="Dry Run", variable=self.dryRunVar, onvalue=1, offvalue = 0)
...
self.useGenreSubFolderVar = IntVar()
self.useGenreSubFolderVar.set(1)
useGenreSubFolderCheckButton = Checkbutton(self.parametersFrame,text="Use genre subfolders", variable=self.useGenreSubFolderVar, onvalue=1, offvalue = 0)
...
是因为变量范围和局部变量被破坏了吗?因为在另一个框架中,我使用本地StringVar来填充一个没有任何问题的条目,它很可能被垃圾收集,因为没有对创建的IntVar的进一步引用。