Save Changed Textbutton State Back To Dictionary Value List Index 0
Good Day. I have managed to load a dictionary from a text file with this code: def loadDictFile(data_folder): critDict = json.loads(open(data_folder+'critDict3.txt').read())
Solution 1:
Your buttons have the same text attribute as the keys in your dictionary, which is very useful. When you declare a tkinter variable and link it to a widget, you can store it in the widget's .var attribute. Later, you can get the widget's text using widget.cget('text') to link the checkbutton and variable to each dictionary key.
defmakeUI(data_folder, critDict):
top = Tk()
checkbuttons = []
for key, val in critDict.items():
myVar = IntVar() # create intVar for this checkbutton
key = Checkbutton(top, text=key, variable=myVar) #create new checkbutton with text
checkbuttons.append(key)
key.var = myVar
key.var.set(val[0])
key.pack()
T = Text(top, height=2, width=30)#create text box
T.pack()
T.insert(END, val[1])#fill text box with comments from dict
text = T.get("1.0",'end-1c')#get text from text box
button=Button(top, text="Update", command=lambda: save(critDict, checkbuttons))
button.pack()
top.mainloop()
Now if you want to access the variable associated with a dictionary key (and it's checkbutton), you can call key.var.get()
, and then save the value to the appropriate dictionary key. Save could look something like this.
defsave(critDict, checkbuttons):
for each in checkbutton:
key = each.cget('text')
critDict[key] = each.var.get()
Post a Comment for "Save Changed Textbutton State Back To Dictionary Value List Index 0"