Issue
I created a simple Python Tkinter gui and I can't manage to have Checkbutton initalize with the correct values, both are always unchecked, no matter what I do, whereas the two prints in the code display :
('dryRunVar', 0)
('useGenreSubFolderVar', 1)
Tried whatever I could with BooleanVar also, and couldn't make it work
Strangely enough if I change instanciation of CheckButton() to ttk.CheckButton(), then both buttons are in 'gray' state
Even changing call to configuration values to constants 0 and 1 doesn't change anything, Buttons stay unchecked
Also try to tweak a little around Tk instanciation and mainloop, no success
#!/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()
Solution
Make your IntVar
an attribute of the class instead.
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)
...
Answered By - Henry Yik
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.