Python (tkinter)- Create Checkbox List From Listbox
I want to create a checkbox list for every listbox item. So, I have a listbox created with 4 different items, 'one', 'two', 'three', 'four'. I want to have a list of corresponding
Solution 1:
Interesting question and I have been working on it for 30 min. There are of course several ways, here the probably shortest and most dynamic:
#!/usr/bin/env python3import tkinter
from tkinter import *
master = tkinter.Tk()
master.geometry("750x500")
listbox = Listbox(master)
listbox.place(x=3,y=0)
enable = ['button 1', 'button 2', 'button 3']
list_for_listbox = ["one", "two", "three", "four"]
for item in list_for_listbox:
listbox.insert(END, item)
for y in enable:
globals()["var{}{}".format(item, y)] = BooleanVar()
globals()["checkbox{}{}".format(item, y)] = Checkbutton(master, text=y, variable=globals()["var{}{}".format(item, y)])
defonselect(evt):
# Note here that Tkinter passes an event object to onselect()
w = evt.widget
x=0
index = int(w.curselection()[0])
value = w.get(index)
print ('You selected item %d: "%s"' % (index, value))
for y in enable:
for item in list_for_listbox:
globals()["checkbox{}{}".format(item, y)].place_forget()
globals()["checkbox{}{}".format(value, y)].place(x=300,y=0+x)
x+=50
listbox.bind('<<ListboxSelect>>', onselect)
print(enable)
mainloop()
You access the vars by globals()["var{}{}".format(item, y)]
example:
for item in list_for_listbox:
for y in enable:
print(item + " [" + y + "] " + str(globals()["var{}{}".format(item, y)].get()))
Solution 2:
Looks like your program needs to remember some state.
You can pass a BooleanVar()
to a Checkbutton
widget, and the widget will keep that BooleanVar
up to date.
#!/usr/bin/env python3import tkinter
from tkinter import *
master = tkinter.Tk()
master.geometry("750x500")
listbox = Listbox(master)
listbox.place(x=3, y=0)
gui_state = {
"one": {
'button 1': BooleanVar(),
'button 2': BooleanVar(),
'button 3': BooleanVar(),
},
"two": {
'button 1': BooleanVar(),
'button 2': BooleanVar(),
'button 3': BooleanVar(),
},
"three": {
'button 1': BooleanVar(),
'button 2': BooleanVar(),
'button 3': BooleanVar(),
},
"four": {
'button 1': BooleanVar(),
'button 2': BooleanVar(),
'button 3': BooleanVar(),
},
}
for item in gui_state.keys():
listbox.insert(END, item)
defbind_checkboxen(master, category):
# render checkboxes for category. Mutate category when checkboxes are toggled.global checkboxes
# delete old checkboxesfor checkbox in checkboxes:
checkbox.destroy()
checkboxes = []
x = 0# create new ones based on category fieldsfor key in category.keys():
checkbox = Checkbutton(master, text=key, variable=category[key])
checkbox.place(x=300, y=0 + x)
checkboxes.append(checkbox)
x += 50
checkboxes = []
defonselect(evt):
# Note here that Tkinter passes an event object to onselect()
w = evt.widget
index = int(w.curselection()[0])
value = w.get(index)
print('You selected item %d: "%s"' % (index, value))
bind_checkboxen(master, gui_state[value])
listbox.bind('<<ListboxSelect>>', onselect)
mainloop()
Post a Comment for "Python (tkinter)- Create Checkbox List From Listbox"