Apr-03-2020, 10:12 AM
Hi!
I have a bunch of ttk.Checkbuttons, and i want to select and deselect all.
I found a solution, but there must be a better/general one.
Thank you,
ifigazsi
for example:
I have a bunch of ttk.Checkbuttons, and i want to select and deselect all.
I found a solution, but there must be a better/general one.
Thank you,
ifigazsi
for example:
import tkinter as tk from tkinter import ttk ############### ROOT ######################### root = tk.Tk() root.title("Pythons") root.geometry('400x100') ############### FUNCTIONS ######################## def tbox_select_all(tbox_list): for i in tbox_list: if i.instate(['selected']) == False: i.invoke() def tbox_deselect_all(tbox_list): for i in tbox_list: if i.instate(['selected']): i.invoke() ############### FRAMES ######################### tickbox_frame = ttk.Frame(root) tickbox_frame.grid(row=0, column=0) ############### CHECKBUTTONS ################### graham_tbox = ttk.Checkbutton(tickbox_frame, text='GRAHAM') john_tbox = ttk.Checkbutton(tickbox_frame, text='JOHN') terry_tbox = ttk.Checkbutton(tickbox_frame, text='TERRY') eric_tbox = ttk.Checkbutton(tickbox_frame, text='ERIC') graham_tbox.grid(row=0, column=1, sticky='w') john_tbox.grid(row=0, column=2, sticky='w') terry_tbox.grid(row=0, column=3, sticky='w') eric_tbox.grid(row=0, column=4, sticky='w') tboxes_list = [graham_tbox, john_tbox, terry_tbox, eric_tbox] for i in tboxes_list: i.invoke() i.invoke() ############## BUTTONS ####################### deselect_all_tbox_button = tk.Button(tickbox_frame, text='Deselect all', command=lambda: tbox_deselect_all(tboxes_list)) deselect_all_tbox_button.grid(row=1, column=2, sticky='e') select_all_tbox_button = tk.Button(tickbox_frame, text='Select all', command=lambda: tbox_select_all(tboxes_list)) select_all_tbox_button.grid(row=1, column=3, sticky='e') root.mainloop()