Python Forum
[Tkinter] ttk.Checkbutton set on/off
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] ttk.Checkbutton set on/off
#1
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:
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()
Reply


Messages In This Thread
ttk.Checkbutton set on/off - by ifigazsi - Apr-03-2020, 10:12 AM
RE: ttk.Checkbutton set on/off - by deanhystad - Apr-03-2020, 01:18 PM
RE: ttk.Checkbutton set on/off - by ifigazsi - Apr-03-2020, 02:23 PM
RE: ttk.Checkbutton set on/off - by deanhystad - Apr-04-2020, 02:54 AM
RE: ttk.Checkbutton set on/off - by ifigazsi - Apr-04-2020, 04:51 PM
RE: ttk.Checkbutton set on/off - by deanhystad - Apr-04-2020, 07:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Checkbutton writing selection to file blakefindlay 1 2,178 Jan-28-2021, 01:56 PM
Last Post: deanhystad
Question [Tkinter] Checkbutton clicks events does not update visually. nicolaask 1 3,148 Dec-20-2020, 06:11 PM
Last Post: nicolaask
  [Tkinter] How to insert 'Checkbutton' to 'Treeview' and associate each item? water 2 13,549 Dec-19-2020, 05:24 PM
Last Post: water
  Find Checkbutton State PEGylated_User 3 3,230 Oct-23-2020, 12:43 AM
Last Post: Larz60+
  [tkinter] not getting checkbutton value when clicked OogieM 5 6,412 Sep-20-2020, 04:49 PM
Last Post: deanhystad
  tkinter checkbutton if checked MC2020 2 6,134 Jan-21-2020, 07:08 PM
Last Post: joe_momma
  [Tkinter] Can't seem to get the current state of a checkbutton. p_hobbs 6 3,492 Nov-07-2019, 11:38 PM
Last Post: p_hobbs
  [Tkinter] Create a set of checkbutton widgets and refer to every one of them individually? Mariano 1 2,770 Apr-11-2019, 06:20 PM
Last Post: woooee
  doing something after been checked a checkbutton gray 1 4,377 Feb-18-2017, 05:11 PM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020