dimanche 6 décembre 2015

Dynamically generated list of checkboxes in Tkinter only returns 0s

I am writing a small gui with Tkinter in python 2,7. At some point I call a function that creates a popup window that is populated by a number of checkboxes, the number of checkboxes is defined by the attributes variable.

def attribute_select(attributes):
popup = tk.Tk()
popup.wm_title("Attribute selection")
label = ttk.Label(popup, text="Please select which of the following \n attributes will undergo k-anonymity.",
                  font=NORMAL_FONT)
label.pack(side="top", fill="x", pady=10)

def read_status(key):
    var_obj = var.get(key)
    print "key is:", key
    print "var_obj.get() is:", var_obj.get()

def leave_mini():
    popup.destroy()

var = dict()
count = 1
for child in range(attributes):
    var[child] = tk.IntVar()
    chk = tk.Checkbutton(popup, text='Attribute: '+str(count), variable=var[child], justify="left", onvalue=1,
                          offvalue=0, command=lambda key=child: read_status(key))
    count += 1
    chk.pack()
print var
exit_button = ttk.Button(popup, text="OK", command=leave_mini)
exit_button.pack()
popup.mainloop()

Everything runs just fine but when I try to check one of the boxes the variable value doesn't change the printout every time is: [ key is: 0 var_obj.get() is: 0 ] or [ key is: 5 var_obj.get() is: 0 ]. So the key is proper for every box but the variable doesn't change. I'm sure it's a simple fix I just can't see it... any ideas?

Aucun commentaire:

Enregistrer un commentaire