ttk::checkbutton
Guide: Themed widgets (ttk)
Makes a themed check button: a box with a text that the user turns on and off.
ttk::checkbutton PATHNAME ?OPTION VALUE ...?
Description
Makes a themed check button with the name PATHNAME and the options of the call, and makes the command PATHNAME for the widget commands below. A click sets the variable of -variable to -onvalue or to -offvalue, and then runs -command.
The button follows its variable: it has the state flag selected while the variable has the value of -onvalue. While the variable does not exist, it has the flag alternate, for a third state.
The parent of PATHNAME must exist, and PATHNAME must not. The class of the button is TCheckbutton, the name of its style. In ztk the button uses the code and the CSS rules of the classic checkbutton. The style Switch.TCheckbutton of ztk draws the button as a switch.
Widget commands
| Command | Description |
|---|---|
ttk::checkbutton invoke | Turns the button over, as a click does, and runs its command. |
ttk::checkbutton state | Gives the state flags of the widget, or turns flags on and off. |
ttk::checkbutton instate | Tests the state flags of the widget, and runs a script when they match. |
ttk::checkbutton identify | Gives the name of the element of the widget at a point. |
ttk::checkbutton cget | Gives the value of an option of the widget. |
ttk::checkbutton configure | Gives or sets the options of the widget. |
Options
| Option | Type | Default | Database name | Description |
|---|---|---|---|---|
-class | class name | empty | The class of the widget for the option database and for the bindings, set only at creation. | |
-command | script | empty | command | The script that runs after a click or invoke changes the variable. |
-compound | compound | empty | compound | Which of the text and the image show, and the place of the image. |
-cursor | cursor name | empty | cursor | The shape of the pointer over the widget. |
-image | image list | empty | image | An image to show, with other images for some states. |
-offvalue | string | 0 | offValue | The value of the variable while the button is off. |
-onvalue | string | 1 | onValue | The value of the variable while the button is on. |
-padding | list of distances | empty | padding | The space between the border and the content, in place of the padding of the style. |
-state | string | normal | state | A state for compatibility with the classic widgets: normal or disabled. |
-style | style name | empty | style | The style of the widget, such as TCheckbutton or a style of the script such as Accent.TCheckbutton. |
-takefocus | boolean, empty, or script | ttk::takefocus | takeFocus | Whether the Tab key can move the focus to the widget. |
-text | string | empty | text | The text of the widget. |
-textvariable | variable name | empty | textVariable | A global variable whose value is the text of the widget. |
-underline | integer | -1 | underline | The index of a character of the text to underline, for a keyboard shortcut. |
-variable | variable name | empty | variable | The global variable that holds the value of the button. |
-width | integer | empty | width | The width of the text area in average characters of the font. |
-class CLASS
The class of the widget for the option database and for the bindings, set only at creation.
The class name replaces the class of the style, such as TCheckbutton, in winfo class and in the bind tags of the widget. Give it in the call that makes the widget. A later configure of the option gives an error.
% ttk::checkbutton .tccl -class Fancy
> winfo class .tccl
Fancy
% ttk::checkbutton .tccl3
> winfo class .tccl3
TCheckbutton% ttk::checkbutton .tccl2
> .tccl2 configure -class Other
attempt to change read-only option
-errorcode NONE-command SCRIPT
The script that runs after a click or invoke changes the variable.
The script runs at the global level after the button sets its variable. Its result is the result of invoke, and an error of the script is the error of invoke. The variable keeps its new value after an error.
% ttk::checkbutton .tccm -variable tccm -command {lappend ::tclog $::tccm}
> .tccm invoke
> .tccm invoke
> set ::tclog
1 0% ttk::checkbutton .tccm2 -variable tccm2 -command {error boom}
> .tccm2 invoke
boom
-errorcode NONE-compound PLACE
Which of the text and the image show, and the place of the image.
An empty value or none shows the image when there is one, else the text. text shows only the text, and image shows only the image. center, top, bottom, left, and right show the two together, with the image at that side of the text, or under it with center.
Values: none, text, image, center, top, bottom, left, right.
% image create photo tcpic -width 40 -height 20
> ttk::checkbutton .tccpi -image tcpic -text Save
> ttk::checkbutton .tccpl -image tcpic -text Save -compound left
> expr {[winfo reqwidth .tccpl] > [winfo reqwidth .tccpi]}
1% ttk::checkbutton .tcco -compound sideways
bad compound "sideways": must be none, text, image, center, top, bottom, left, or right
-errorcode TCL LOOKUP INDEX compound sideways-cursor CURSOR
The shape of the pointer over the widget.
Each value side by side: the contact sheet of the cursors
A cursor name of Tk, such as hand2 or xterm. An empty value uses the cursor of the parent.
% ttk::checkbutton .tccu -cursor hand2
> .tccu cget -cursor
hand2ztk stores each value of the option with no check.
-image IMAGESPEC
An image to show, with other images for some states.
The first word is the name of an image of image create. More words are pairs of a state spec and an image, such as {pic disabled pic2}. With an image and -compound none, the widget shows the image and no text. The image must exist when the option is set.
% image create photo tcicon -width 40 -height 20
> ttk::checkbutton .tcim -image tcicon
> expr {[winfo reqheight .tcim] >= 20}
1% ttk::checkbutton .tcim3 -image noimage
image "noimage" doesn't exist
-errorcode TK LOOKUP IMAGE noimage-offvalue VALUE
The value of the variable while the button is off.
A click on a button that is on sets the variable to this value.
% ttk::checkbutton .tcof -variable tcof -onvalue yes -offvalue no
> .tcof invoke
> .tcof invoke
> set tcof
noEach string is a valid value.
-onvalue VALUE
The value of the variable while the button is on.
A click on a button that is off sets the variable to this value. The button has the flag selected while the variable has this value.
% ttk::checkbutton .tcon -variable tcon -onvalue yes -offvalue no
> set tcon yes
> .tcon instate selected
1Each string is a valid value.
-padding PADDING
The space between the border and the content, in place of the padding of the style.
A list of 1 to 4 distances: left, top, right, and bottom. One value is the space at each side. Two values are the left and right space and the top and bottom space. A missing third value is the first, and a missing fourth value is the second. An empty value uses the padding of the style.

-padding {}
-padding 4
-padding {16 4}-text Agree -variable ::ref::off.% ttk::checkbutton .tcpa -text Hi
> ttk::checkbutton .tcpa30 -text Hi -padding 30
> expr {[winfo reqwidth .tcpa30] > [winfo reqwidth .tcpa]}
1ztk stores each value at configure, as Tk 8.6.16 does.
-state STATE
A state for compatibility with the classic widgets: normal or disabled.
disabled sets the state flag disabled of the widget, and normal clears it. The state command changes the flags with no change of this option. ztk stores each other value with no check.

-state normal
-state disabled-text Agree -variable ::ref::off.% ttk::checkbutton .tcst -state disabled
> .tcst instate disabled
1
# The `state` command does not change the option.
% ttk::checkbutton .tcst2
> .tcst2 state disabled
> .tcst2 cget -state
normalztk stores each value of the option with no check, as Tk 8.6.16 does.
-style STYLE
The style of the widget, such as TCheckbutton or a style of the script such as Accent.TCheckbutton.
An empty value uses the style TCheckbutton. A style name of the script ends with the name of the base style, such as Accent.TCheckbutton, and ttk::style configure and ttk::style map give it its options. A style with no settings looks like its base style.

-style TCheckbutton
-style Switch.TCheckbutton-text Agree -variable ::ref::off.% ttk::style configure Accent.TCheckbutton -foreground white
> ttk::checkbutton .tcsy -style Accent.TCheckbutton
> .tcsy cget -style
Accent.TCheckbuttonztk stores each value of the option with no check, as Tk 8.6.16 does at creation.
-takefocus VALUE
Whether the Tab key can move the focus to the widget.
1 puts the widget in the order of the Tab key, and 0 leaves it out. An empty value uses the default rule of Tk. A script, such as the default ttk::takefocus, runs with the path of the widget appended, and its result is the answer. ttk::takefocus gives 1 when the widget is not disabled and is viewable.
% ttk::checkbutton .tctf -takefocus 0
> .tctf cget -takefocus
0ztk stores each value of the option with no check.
-text TEXT
The text of the widget.
The text that the widget shows. A newline starts a new line. A longer text makes a wider widget, unless -width sets the width. With -textvariable, the variable sets the text.

-text Agree
-text {Send me the news}-variable ::ref::off.% ttk::checkbutton .tctx -text Save
> set w [winfo reqwidth .tctx]
> .tctx configure -text "Save as"
> expr {[winfo reqwidth .tctx] > $w}
1Each string is a valid text.
-textvariable VAR
A global variable whose value is the text of the widget.
The widget shows the value of the global variable VAR in place of -text, and changes its text when the variable changes. The variable stays after the widget is destroyed.
% set tctitle Open
> ttk::checkbutton .tctv -textvariable tctitle
> set w [winfo reqwidth .tctv]
> set tctitle "Open a file"
> expr {[winfo reqwidth .tctv] > $w}
1Each name is a valid variable name.
-underline INDEX
The index of a character of the text to underline, for a keyboard shortcut.
The index from 0 of the character of -text to underline. -1 underlines no character. The underline is a hint for a shortcut key, and the script binds the key itself.
% ttk::checkbutton .tcun -text Save -underline 0
> .tcun cget -underline
0% ttk::checkbutton .tcun2 -underline x
expected integer but got "x"
-errorcode TCL VALUE NUMBER-variable VAR
The global variable that holds the value of the button.
The button sets the variable to -onvalue or -offvalue, and shows the value that the variable has. The default is a variable with the name of the path of the widget, such as ::.wifi. The button does not set the variable before the first click.
% ttk::checkbutton .tcva
> list [.tcva cget -variable] [info exists ::.tcva]
.tcva 0
% ttk::checkbutton .tcva2 -variable tcva2
> set tcva2 1
> .tcva2 state
selectedEach name is a valid variable name.
-width CHARS
The width of the text area in average characters of the font.
A positive value sets the width of the text area. A negative value sets a minimum width, and a longer text makes the widget wider. An empty value makes the widget as wide as its content.
% ttk::checkbutton .tcwi -text Hi
> ttk::checkbutton .tcwi10 -text Hi -width 10
> expr {[winfo reqwidth .tcwi10] > [winfo reqwidth .tcwi]}
1ztk stores each value at configure, as Tk 8.6.16 does.
Bindings
| Event | Description |
|---|---|
<Enter> | The pointer is over the button: the theme draws the hover look (the state active). |
<Leave> | The pointer leaves the button, and the hover look goes. |
<Button-1> | A press switches the variable between -onvalue and -offvalue and runs the -command script at once, as invoke does. A press on a disabled button changes no value. |
<Key-space> | Switches the variable and runs the -command script, as invoke does. |
<Key-Return> | The same as the space key. |
Virtual events
| Event | Description |
|---|---|
<<Invoke>> | The same as the space key. event generate .c <<Invoke>> sends it. |
States
| State | Description |
|---|---|
selected | The variable holds the -onvalue: the theme draws a check mark. |
alternate | The variable does not exist, or holds an empty string: the theme draws a dash, the mixed state. |
active | The pointer is over the button. hover is the same flag in ztk. |
pressed | Button 1 is down on the button. |
focus | The button has the keyboard focus. |
disabled | The button changes no value, and the theme draws it grayed. |

no state flag
state selected
state alternate
state active
state focus
state disabled
state selected disabled-text Agree -variable ::ref::off. ::ref::off holds 0, the -offvalue, so the button with no flag is clear.Returns
PATHNAME.
Examples
% ttk::checkbutton .wifi -text "Wi-Fi" -variable wifi
> .wifi invoke
> set wifi
1
% winfo class [ttk::checkbutton .bt -text Bluetooth]
TCheckbutton
Errors
% ttk::checkbutton .x -foo b
unknown option "-foo"
-errorcode TK LOOKUP OPTION -foo
% ttk::checkbutton .twice
> ttk::checkbutton .twice
window name "twice" already exists in parent
-errorcode TK VALUE WINDOW EXISTS
# The parent .no does not exist.
% ttk::checkbutton .no.c
bad window path name ".no"
-errorcode TK LOOKUP WINDOW .no
% ttk::checkbutton
wrong # args: should be "ttk::checkbutton pathName ?-option value ...?"
-errorcode TCL WRONGARGS
Differences from Tcl
-cursoraccepts each value with no check. Tk 8.6.16 gives an error for a cursor name that does not exist.- The errors of
state,instate,identify, and of an unknown widget command have the error codeNONE. Tk 8.6.16 gives a code such asTTK VALUE STATEorTCL LOOKUP INDEX. The error of-underlinehas the codeTCL VALUE NUMBER, where Tk 8.6.16 givesTCL VALUE INTEGER. - The states
activeandhoverare one flag, sostate activeturns on both. - When the variable is unset after the button is made, the button does not turn on the flag
alternate. Tk 8.6.16 turns it on. - The error of a wrong count of arguments of
invokehas the error codeNONE. Tk 8.6.16 givesTCL WRONGARGS. - A press of button 1 changes the value. In Tk 8.6.16 the release changes it, and a release away from the button changes no value.
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztk 1.0.0-beta.1