Checkbuttons gets generated dynamically and they are getting text from a python list.
I need a logic for capturing selected checkbuttons text .
As per my research everywhere they are returning the state of checkbox instead of text.
Please help.
cb_list =['pencil','pen','book','bag','watch','glasses','passport','clothes','shoes','cap']
try:r = 0cl = 1for op in cb_list:cb = Checkbutton(checkbutton_frame, text=op, relief=RIDGE)cb.grid(row=r, column=cl, sticky="W")r = r + 1
except Exception as e:logging.basicConfig(filename=LOG_FILENAME, level=logging.ERROR)logging.error(e)# print (e)selected_item = Text(self, width=30, height=20, wrap=WORD)selected_item.grid(row=1, column=6, padx=20, pady=20, columnspan=2, sticky=E)display_button = Button(self, text='DISPLAY', command=display()convert_button.grid(row=1, column=8, padx=20, pady=20)
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 tkroot = tk.Tk()
checkbutton_frame = tk.Frame(root)
checkbutton_frame.grid(row=1, column=0)def display():# clear listboxselected_item.delete(0, 'end')# add selected items in listboxfor text, var in zip(cb_list, cb_var):if var.get():# the checkbutton is selectedselected_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 tkroot = tk.Tk()
checkbutton_frame = tk.Frame(root)
checkbutton_frame.grid(row=1, column=0)def display(cb_list, cb_var):# clear listboxselected_item.delete(0, 'end')# add selected items in listboxfor text, var in zip(cb_list, cb_var):if var.get():# the checkbutton is selectedselected_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 listboxselected_item.delete(0, 'end')# create new checkbuttonscb_var = [] # to store the variables associated to the checkbuttonsfor 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 commanddisplay_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()