I am writing a small Tkinter/Python program, that has a list of checkboxes with variable length (determined at run time).
I want to be able to read the state of all the checkboxes at any time, but I am not sure how I should go about that.
Here's the code snippet for generating the list (adopted from this post):
def relist(self):
self.text.delete(1.0,END)
p = subprocess.Popen (['ls', '/dev/'], stdout = subprocess.PIPE)
lst = p.communicate()[0].split('\n')
print lst
for item in lst:
v = tk.IntVar()
cb = tk.Checkbutton(text="/dev/%s" % item, variable=v, command=self.cb(index))
self.text.window_create("end", window=cb)
self.text.insert("end", "\n") # to force one checkbox per line
And my dummy handler:
def cb(self,idx):
print ("var is %s", str(idx))
lst[idx] = 1;
The problem is that my handler is getting called once (when the Checkbuttons are created), whereas I want it to get called everytime a Checkbutton
is clicked (checked or unchecked), and when it is called, I want it to update lst.