I am creating a GUI with checkbuttons and will use the state of each button to determine if another method is called later in my script.
I am going this via a loop, however when I disable or enable any checkbutton, it then controls the state of all the rest.
Here is the snippet of code:
#List of checkbutton text
self.source_list = ["a","b","c","d","e","f","g","h","i","j","k"]
#The variable for each text button which will be used later in the script to determine which method to call e.g. if self.source_var[i].get() == True: do something
self.source_var = []
self.var = BooleanVar(value=1)
j = 0
k = 0
#loop to create checkbttons
for i in range(len(self.source_list)):
self.source_var.append(self.var) #add var to list
ttk.Checkbutton(self.window, text = self.source_list[i], var = self.source_var[i]).grid(row=k,column=j,rowspan=1,sticky=W)
k += 1
#new column
if k == 4:
k = 0
j += 1
I am pretty new to python so I could be doing something very simple wrong but cannot find out what it is after research on the internet for the last hour.
Thanks in advance!
SOLUTION thanks to @jizhihaoSAMA
#List of checkbutton text
self.source_list = ["a","b","c","d","e","f","g","h","i","j","k"]
# #The variable for each text button which will be used later in the script to determine which method to call e.g. if self.source_var[i].get() == True: do something
self.source_var = []
j = 0
k = 0
#loop to create checkbttons
for i in range(len(self.source_list)):
self.var = BooleanVar(value=1)
self.source_var.append(self.var) #add var to list
ttk.Checkbutton(self.window, text = self.source_list[i], var = self.source_var[i]).grid(row=k,column=j,rowspan=1,sticky=W)
k += 1
#new column
if k == 4:
k = 0
j += 1
#Dictionary for checking variable state later
self.source_dict = dict(zip(self.source_list,self.source_var))
if self.source_dict.get("a").get()) == True:
#do XYZ