Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Checkbutton state

How do I get the 'state' of a Tkinter Checkbutton? By 'state' I mean get whether or not it has a check mark in it or not.

like image 988
rectangletangle Avatar asked Nov 21 '10 07:11

rectangletangle


People also ask

How to get Checkbutton state in Tkinter?

In terms of a particular application, we can check the state of the tkinter CheckButtons by using the state() method. It prints the actual state of the tkinter checkbuttons.

How do I find the value of my Checkbutton?

IntVar(). get() returns the onvalue or ofvalue of the Checkbutton, based on the status. You can either assign a function to command option of Checkbutton, and read the status of Checkbutton as and when the button is checked or not.

How do you uncheck a checkbox in Python?

Once the checkbox is selected, we are calling prop() function as prop( "checked", true ) to check the checkbox and prop( "checked", false ) to uncheck the checkbox.

What is IntVar () in tkinter?

_ClassType IntVarConstruct an integer variable. set(self, value) Set the variable to value, converting booleans to integers. get(self) Return the value of the variable as an integer.


2 Answers

When you're creating it, it takes a variable keyword argument. Pass it an IntVar from Tkinter. Checking or unchecking the box will set that value contained by var to the corresponding boolean state. This can be accessed as var.get():

checked => var.get()

not checked => not var.get()

>>> root = Tkinter.Tk() >>> var = Tkinter.IntVar() >>> chk = Tkinter.Checkbutton(root, text='foo', variable=var) >>> chk.pack(side=Tkinter.LEFT) >>> var.get()  #unchecked 0 >>> var.get()  #checked 1 
like image 76
aaronasterling Avatar answered Sep 19 '22 15:09

aaronasterling


If you use the new* ttk module from tkinter, you can read and write checkbutton states without assigning variables.

import tkinter from tkinter import ttk tkwindow = tkinter.Tk() chk = ttk.Checkbutton(tkwindow, text="foo") chk.grid(column=0, row=0) 

Notice that the new checkbox defaults to the "alternate", sometimes called "half-checked", state:

alternate

You can read the current state usinge the .state() method:

>>> print(chk.state())  # half-checked ('alternate',) >>> print(chk.state())  # checked ('selected',) >>> print(chk.state())  # not checked () 

To set the state in code:

chk.state(['selected'])  # check the checkbox chk.state(['!selected']) # clear the checkbox chk.state(['disabled'])  # disable the checkbox chk.state(['!disabled','selected']) # enable the checkbox and put a check in it! 

And here is a convenient way to check for a specific state:

chk.instate(['selected'])  # returns True if the box is checked 

There are two tricky things I've found:

  1. The initial state is "alternate", and this state flag doesn't get cleared when adding a "selected" state flag. So, if you want to toggle your checkbutton in code, you'll first need to clear the "alternate" flag:

    chk.state(['!alternate']) 
  2. If you disable/enable the checkbutton using

    chk.state(['disabled']) chk.state(['!disabled']) 

    then everything works fine. But, if you use these common, alternate methods:

    chk.config(state=tk.DISABLED) chk.config(state=tk.NORMAL) 

    then it reasserts the 'alternate' flag.

    This behavior doesn't happen if you assign a variable to the checkbutton, but then, if you wanted to assign a variable then this answer probably won't help you :)


* ttk became available in Python 2.7 (Tk 8.5). This question talks about the differences between the old standard widgets and the newer, "themed" ones.

like image 26
bitsmack Avatar answered Sep 18 '22 15:09

bitsmack