ttk create multiple checkbuttons

206 views Asked by At

I'm trying to make a GUI that display lots of checkbuttons, i create them from a list; make a dictionary form the list and assign each checkbutton a variable from the dictionary so i can check it's state later. Problem is that all the checkbuttons are displayed in an 'alternate' state, even if i set the variable to either 0 or 1, i've also tried changing states, but nothing seems to help.

y = 0
        for x in get_dir_names(r'D:\SKL\test\win10'):
            drv_check[x] = Variable()
            drv_check[x].set(0)
            center_window(150, 500, top_child)
            drv = ttk.Checkbutton(child_frame, text=x, variable=drv_check[x])
            drv.grid(row=y, column=0, sticky=W)
            y += 1

for reference

def get_dir_names(dir_path):
    """Get names only of all directories from a given path (none recursive)"""
    drv_list = [x for x in os.walk(dir_path).__next__()[1]]
    drv_name = dict({})
    for y in drv_list:
        tmp_ver = dir_path + r'\\' + y
        drv_name[y] = (os.walk(tmp_ver).__next__()[1]).pop()
    return drv_name
1

There are 1 answers

0
Alex Zel On BEST ANSWER

Figured it out, I've made a "toggle all" button and it seemed to fix it, but it's weird that it didn't work before.

here's the function i used:

def toggle_all(*args):
        while True:
            if toggle_all_var.get() == '1':
                for name in drv_check:
                    drv_check[name].set('1')
            elif toggle_all_var.get() == '0':
                for name in drv_check:
                    drv_check[name].set('0')

ttk.Checkbutton(drv_frame, text='Toggle all', variable=toggle_all_var).grid(row=y, column=0, sticky=W)

Also i run the function in a new thread.