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
#2
What don't you like about your solution? Is it that you had to create a list of widgets that are checked/unchecked?

The normal way something like this is done is to modify the underlaying data then update the window to display the modified data. The window should be thought of as a view of the data. When the data changes the view should reflect the change.

If you are looking for more generic ways of accessing widgets in a window, take a look at winfo_children(). This will return a list of widgets that are children of the window. You could then determine which ones are check boxes and uncheck them. You could also try the widget.children dictionary. Each widget has a dictionary of it's children so widget.children.values() would give you a list of all children.
Reply
#3
Hi!

@Larz60+ : thank you, for formatting my post.

[/python][/quote]
(Apr-03-2020, 01:18 PM)deanhystad Wrote: What don't you like about your solution?

In "plain" tkinter there was: .select()/.deselect() option, which was straitforward.

In my solution, i'm checking the state first, then modifying it if its needed. (extra if simply bothers me).
Reply
#4
I don't think you are supposed to set or get values from the widget. It looks like you ttk expects you to create a variable and bind it to the control. Changes to the value change the appearance of the control, and manipulating the control changes the value of the variable. In the example below I make two columns of checkbuttons. Each row shares the same variable, so changing one check box changes the other. At the bottom are a couple of buttons for checking/unchecking all the buttons. None of these operation use the widget directly which I toss away as soon as it is added to the form.
from functools import partial
from tkinter import *
from tkinter.ttk import *

window = Tk()
window.title = 'Checkbuttons'

variables = []

def set_all(value):
    for v in variables:
        v.set(value)

for i in range(1, 5):
    v = BooleanVar()
    variables.append(v)
    Checkbutton(window, text='Check button'+str(i), variable=v).grid(column=0, row=i)
    Checkbutton(window, text='Check button'+str(i+4), variable=v).grid(column=1, row=i)

Button(window, text='Check All', command = partial(set_all, True)).grid(column=0, row=6)
Button(window, text='Uncheck All', command = partial(set_all, False)).grid(column=1, row=6)

window.mainloop()
If you are planning on writing GUI applications in python you should experiment with using pakages other than Tk/tkinter/ttk. I played with Tk a little bit before moving on to wxPython and now PySide2. I've also used GTK outside of python. Of those I liked Tk the least by a wide margin.
Reply
#5
(Apr-04-2020, 02:54 AM)deanhystad Wrote: I don't think you are supposed to set or get values from the widget. It looks like you ttk expects you to create a variable and bind it to the control. Changes to the value change the appearance of the control, and manipulating the control changes the value of the variable.

Ok, i got, thank you! :)

(Apr-04-2020, 02:54 AM)deanhystad Wrote: If you are planning on writing GUI applications in python you should experiment with using pakages other than Tk/tkinter/ttk. I played with Tk a little bit before moving on to wxPython and now PySide2. I've also used GTK outside of python. Of those I liked Tk the least by a wide margin.

Which one do you prefer? (for a newbie?) I mostly work with large data sets, and only minor GUI is needed.

Thank you,
ifigazsi
Reply
#6
Doesn't mater what I like. I used Tcl/Tk back in the early 90's and even ttk is showing it's age. I am using PySide2 right now it is ok. Qt5 is nearly identical. You can build your panels using a designer app and bind the controls to your data. That is not what I am doing. My interest is more in making toolkit for others to use to build applications. GTK is a lot like Qt (and wxPython for that matter). wxPython is supposed to look just like whatever platform you are running on (Qt does this somewhat too but I hear not quite as well), so you can write on a Mac and it looks like a Windows program when run on WIndows or a whatever program when run on Linux. However wxPython has a limited set of widgets when compared to most other gui's. Probably not a problem for you. I hear a lot of good stuff about Kivy. It is supposed to be good for putting together interfaces really fast. It is next on my list of GUI toolkits to try,
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Checkbutton writing selection to file blakefindlay 1 2,078 Jan-28-2021, 01:56 PM
Last Post: deanhystad
Question [Tkinter] Checkbutton clicks events does not update visually. nicolaask 1 2,919 Dec-20-2020, 06:11 PM
Last Post: nicolaask
  [Tkinter] How to insert 'Checkbutton' to 'Treeview' and associate each item? water 2 12,994 Dec-19-2020, 05:24 PM
Last Post: water
  Find Checkbutton State PEGylated_User 3 3,047 Oct-23-2020, 12:43 AM
Last Post: Larz60+
  [tkinter] not getting checkbutton value when clicked OogieM 5 5,882 Sep-20-2020, 04:49 PM
Last Post: deanhystad
  tkinter checkbutton if checked MC2020 2 5,925 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,267 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,675 Apr-11-2019, 06:20 PM
Last Post: woooee
  doing something after been checked a checkbutton gray 1 4,236 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