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
#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


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,149 Jan-28-2021, 01:56 PM
Last Post: deanhystad
Question [Tkinter] Checkbutton clicks events does not update visually. nicolaask 1 3,065 Dec-20-2020, 06:11 PM
Last Post: nicolaask
  [Tkinter] How to insert 'Checkbutton' to 'Treeview' and associate each item? water 2 13,385 Dec-19-2020, 05:24 PM
Last Post: water
  Find Checkbutton State PEGylated_User 3 3,162 Oct-23-2020, 12:43 AM
Last Post: Larz60+
  [tkinter] not getting checkbutton value when clicked OogieM 5 6,265 Sep-20-2020, 04:49 PM
Last Post: deanhystad
  tkinter checkbutton if checked MC2020 2 6,077 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,418 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,732 Apr-11-2019, 06:20 PM
Last Post: woooee
  doing something after been checked a checkbutton gray 1 4,331 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