close

Modern Tkinter

Modern Tkinter ebook

Want TkDocs to go? If you've found the material on this site useful, please consider buying this book, which is based on the Python material in the tutorial.


Mark Roseman

p.s. your purchase helps me improve this site... thank you!

 

Only $10 for e-book. pick your format:

Paperback PDF Kindle Apple Books B&N Nook Kobo

(Slightly more for paperback. Also available on other e-book platforms.)

The widget roundup provides you with a quick and easy reference to the most important features and options for each Tk widget.

Checkbutton

Widget Essentials

When to use: To allow the user to choose between two mutually exclusive choices.
Tcl:ttk::checkbutton .cb -text "Self-destruct on close" 
   -variable destructFlag
Ruby:$destructFlag = TkVariable.new ( initialValue );
cb = Tk::Tile::Checkbutton.new(parent) { 
   text "Self-destruct on close"; variable $destructFlag}
Perl:my $cb = parent->new_ttk__checkbutton
   -text => "Self-destruct on close", -variable => $destructFlag);
Python:destructFlag = StringVar()
cb = ttk.Checkbutton(parent, text="Self-destruct on close", 
   variable=destructFlag)
Reference:(at www.tcl.tk)

Common Options

text The text label to be shown next to the checkbox.
variable A variable linked to the checkbutton; when the variable is changed, the checkbutton will reflect the new value, while if the user toggles the checkbutton, the variable's value will be updated.
onvalue,
offvalue
Values reflecting the checked (default 1) and unchecked (default 0), which are held in the linked variable. If the variable contains neither of these values, the checkbutton is put into an indeterminate state, which usually implies that the choice has not yet been made (and that a default choice is inappropriate).

How do I...

See if the checkbutton is checked? Look at the linked variable.
Check or uncheck it? Change the linked variable to either the onvalue or offvalue.
See if it's in the indeterminate state? Check if the variable holds a value other than the onvalue and offvalue.
You can also use the "instate alternate" method.
Disable the checkbutton? Use the "state disabled" method.
You can reenable this with "state !disabled".
Check with "instate disabled" (returns 1 if disabled, else 0).

Less Common Options

textvariable As an alternative to text, get the string from a variable, updating when the variable changes.
command The script to invoke when the checkbutton is pressed.
image,
compound
Specify a Tk Image object (not the path to an image file) instead of the text label. If compound is center, top, bottom, left, or right, display the text and the image.