Exactly what the questions says, and I have no idea why. Basically my intention is to initialise all the checkbuttons as unchecked. Here is my code:
import tkinter as tk
from tkinter import ttk
class Checkbuttons():
def __init__(self,parent,list_of_values,column,row,**kwargs):
#Converts list into dictionary
self.values = {}
for value in list_of_values:
self.values[value] = tk.IntVar()
column_counter = column
row_counter = row
for tag in list(self.values):
print('{} : {}'.format(tag,self.values[tag].get())) #Test reveals IntVar is 0
Checkbutton(parent,tag,self.values[tag],column_counter,row_counter,width=15)
print('{} : {}'.format(tag,self.values[tag].get())) #Test reveals IntVar is 0
self.values[tag].set(0)
row_counter += 1
class Checkbutton(ttk.Checkbutton):
def __init__(self,parent,text,variable,column,row,search=True,**kwargs):
kwargs['text'] = text
kwargs['variable'] = variable
super().__init__(parent,**kwargs)
print(variable.get()) #IntVar is also zero here.
self.grid(column=column,row=row)
root = tk.Tk()
my_list = {'e-book epub','e-book PDF','Paperback','Hardcover','Audiobook MP3','Audible','Kindle'}
Checkbuttons(root,my_list,1,1)
root.mainloop()
Any help is greatly appreciated, and please do not criticise other aspects of my code unless it is relevant.
The issue is caused by the line:
Checkbuttons(root,my_list,1,1)
Since there is no variable reference the instance of Checkbuttons()
, it will be garbage collected (also its instance variable self.values
as well).
You need to use a variable to hold the instance:
a = Checkbuttons(root,my_list,1,1)