The idea is to associate one BooleanVar
to each checkbutton and store them in a list cb_var
. Then, to display the selected items, we just have to clear the display box (I have used a Listbox
) and then loop simultaneously through cb_list
and cb_var
to determine which items are selected:
import tkinter as tk
root = tk.Tk()
checkbutton_frame = tk.Frame(root)
checkbutton_frame.grid(row=1, column=0)
def display():
# clear listbox
selected_item.delete(0, 'end')
# add selected items in listbox
for text, var in zip(cb_list, cb_var):
if var.get():
# the checkbutton is selected
selected_item.insert('end', text)
cb_list = ['pencil','pen','book','bag','watch','glasses','passport','clothes','shoes','cap']
cb_var = [] # to store the variables associated to the checkbuttons
cl = 1
for r, op in enumerate(cb_list):
var = tk.BooleanVar(root, False)
cb = tk.Checkbutton(checkbutton_frame, variable=var, text=op, relief='ridge')
cb.grid(row=r, column=cl, sticky="w")
cb_var.append(var)
selected_item = tk.Listbox(root, width=30, height=20)
selected_item.grid(row=1, column=6, padx=20, pady=20, columnspan=2, sticky='e')
display_button = tk.Button(root, text='DISPLAY', command=display)
display_button.grid(row=1, column=8, padx=20, pady=20)
root.mainloop()

EDIT: If you want to be able to change the list of items easily, you can use a function init_checkbuttons
to create the checkbuttons from your list
of items. This function does the following things:
- Destroy all previous checkbuttons
- Clear the listbox
- Create the new checkbuttons
- Change the command of the display button
You can notice that the display
function now takes cb_list
and cb_var
in argument, so that you can change them.
import tkinter as tk
root = tk.Tk()
checkbutton_frame = tk.Frame(root)
checkbutton_frame.grid(row=1, column=0)
def display(cb_list, cb_var):
# clear listbox
selected_item.delete(0, 'end')
# add selected items in listbox
for text, var in zip(cb_list, cb_var):
if var.get():
# the checkbutton is selected
selected_item.insert('end', text)
def init_checkbuttons(cb_list, cl=1):
# destroy previous checkbuttons (assuming that checkbutton_frame only contains the checkbuttons)
cbs = list(checkbutton_frame.children.values())
for cb in cbs:
cb.destroy()
# clear listbox
selected_item.delete(0, 'end')
# create new checkbuttons
cb_var = [] # to store the variables associated to the checkbuttons
for r, op in enumerate(cb_list):
var = tk.BooleanVar(root, False)
cb = tk.Checkbutton(checkbutton_frame, variable=var, text=op, relief='ridge')
cb.grid(row=r, column=cl, sticky="w")
cb_var.append(var)
# change display command
display_button.configure(command=lambda: display(cb_list, cb_var))
cb_list = ['pencil', 'pen', 'book', 'bag', 'watch', 'glasses', 'passport', 'clothes', 'shoes', 'cap']
cb_list2 = ['ball', 'table', 'bat']
selected_item = tk.Listbox(root, width=30, height=20)
selected_item.grid(row=1, column=6, padx=20, pady=20, columnspan=2, sticky='e')
display_button = tk.Button(root, text='DISPLAY')
display_button.grid(row=1, column=8, padx=20, pady=20)
tk.Button(root, text='Change list', command=lambda: init_checkbuttons(cb_list2)).grid(row=2, column=8)
init_checkbuttons(cb_list)
root.mainloop()