2

Using following code I try to get updated list of checkbuttons' corresponding text values, everytime checkbutton is checked or unchecked:

import Tkinter as tk

opt = []
def chkbox_checked():
    for ix, item in enumerate(cb):
        opt.append(cb_v[ix].get())
    print opt
root = tk.Tk()
mylist = [
'NR',
'ECEF X',
'ECEF Y',
'ECEF Z',
'height'
]
cb = []
cb_v = []
for ix, text in enumerate(mylist):
    cb_v.append(tk.StringVar())
    cb.append(tk.Checkbutton(root, text=text, onvalue=text, variable=cb_v[ix],   comand=chkbox_checked))
    cb[ix].grid(row=ix, column=0, sticky='w')   
label = tk.Label(root, width=20)
label.grid(row=ix+1, column=0, sticky='w')
root.mainloop()

If for example all buttons are checked from the first to the last, my desired output would be:

['NR']
['NR','ECEF X]
['NR','ECEF X','ECEF Y']
['NR','ECEF X','ECEF Y','ECEF Z]
['NR','ECEF X','ECEF Y','ECEF Z','height',]

but with above code I get multiplied output and also there's something wrong with checkbuttons themselves, their state is checked from the beginning. Any help would be appreciated.

1 Answer 1

7

One problem with the above is the opt.append in chkbox_checked ... Since this function gets called everytime a button is checked/unchecked, the length of the opt list will increase by the number of checkbuttons you have everytime one of the buttons is clicked. The solution (posted below) is to initialize opt when you create the buttons and then just update it's elements in chkbox_checked. As far as the state of the buttons on creation, I'm not sure why they're initially checked, but you can easily deselect the buttons at initialization as well using the deselect method.

import Tkinter as tk

opt = []
def chkbox_checked():
    for ix, item in enumerate(cb):
        opt[ix]=(cb_v[ix].get())
    print opt
root = tk.Tk()  
mylist = [
'NR',
'ECEF X',
'ECEF Y',
'ECEF Z',
'height' 
]
cb = []
cb_v = []
for ix, text in enumerate(mylist):
    cb_v.append(tk.StringVar())
    off_value=0  #whatever you want it to be when the checkbutton is off
    cb.append(tk.Checkbutton(root, text=text, onvalue=text,offvalue=off_value,
                             variable=cb_v[ix],
                             command=chkbox_checked))
    cb[ix].grid(row=ix, column=0, sticky='w')
    opt.append(off_value)
    cb[-1].deselect() #uncheck the boxes initially.
label = tk.Label(root, width=20)
label.grid(row=ix+1, column=0, sticky='w')
root.mainloop()

Another trick that may be useful is instead of keeping 2 lists (cb and cb_v), you could just add the StringVars as attributes to your checkbuttons. e.g.:

v=tk.StringVar()
cb.append(tk.CheckButton(... , variable=v, ...)
cb[-1].v=v

Then you just have one list with all the data. The corresponding chkbox_checked would look like:

def chkbox_checked():
   opt=[chkbox.v.get() for chkbox in cb]
   print opt

(Note this also eliminates the need for a global opt list ... although there are probably a whole bunch of other ways to get rid of that list)

Sign up to request clarification or add additional context in comments.

4 Comments

Is it possible any easy way to prevent keeping offvalues in updated list and have only ['NR'] not ['NR', '0', '0', '0', '0'] if for example first button is checked?
Sure ... The easiest way is to just pull the '0' out of the list. For example, if you use the latter chkbox_checked function above, you cn simply change it to: opt=[chkbox.v.get() for chkbox in cb if chkbox.v.get != '0' ]
One last question: how could I update column value +1 for every two items in ix?, simply I would like to put checkbuttons in three columns.
I'm not completely sure that I understand what you're asking, but I'll give it a shot... If you want your checkbuttons in 3 rows, you can do something like: cb[ix].grid(row=int(ix/3),column=ix%3) ... This will keep the row value constant for 3 values of ix and increment the column value up to 3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.