beetlebug▾
/

ttk::checkbutton

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

CommandDescription
ttk::checkbutton invokeTurns the button over, as a click does, and runs its command.
ttk::checkbutton stateGives the state flags of the widget, or turns flags on and off.
ttk::checkbutton instateTests the state flags of the widget, and runs a script when they match.
ttk::checkbutton identifyGives the name of the element of the widget at a point.
ttk::checkbutton cgetGives the value of an option of the widget.
ttk::checkbutton configureGives or sets the options of the widget.

Options

OptionTypeDefaultDatabase nameDescription
-classclass nameemptyThe class of the widget for the option database and for the bindings, set only at creation.
-commandscriptemptycommandThe script that runs after a click or invoke changes the variable.
-compoundcompoundemptycompoundWhich of the text and the image show, and the place of the image.
-cursorcursor nameemptycursorThe shape of the pointer over the widget.
-imageimage listemptyimageAn image to show, with other images for some states.
-offvaluestring0offValueThe value of the variable while the button is off.
-onvaluestring1onValueThe value of the variable while the button is on.
-paddinglist of distancesemptypaddingThe space between the border and the content, in place of the padding of the style.
-statestringnormalstateA state for compatibility with the classic widgets: normal or disabled.
-stylestyle nameemptystyleThe style of the widget, such as TCheckbutton or a style of the script such as Accent.TCheckbutton.
-takefocusboolean, empty, or scriptttk::takefocustakeFocusWhether the Tab key can move the focus to the widget.
-textstringemptytextThe text of the widget.
-textvariablevariable nameemptytextVariableA global variable whose value is the text of the widget.
-underlineinteger-1underlineThe index of a character of the text to underline, for a keyboard shortcut.
-variablevariable nameemptyvariableThe global variable that holds the value of the button.
-widthintegeremptywidthThe 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.

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
hand2

ztk 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
no

Each 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
1

Each 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.

ttk::checkbutton with -text Agree -variable ::ref::off and -padding {}-padding {}ttk::checkbutton with -text Agree -variable ::ref::off and -padding 4-padding 4ttk::checkbutton with -text Agree -variable ::ref::off and -padding {16 4}-padding {16 4}
Each with -text Agree -variable ::ref::off.
% ttk::checkbutton .tcpa -text Hi
> ttk::checkbutton .tcpa30 -text Hi -padding 30
> expr {[winfo reqwidth .tcpa30] > [winfo reqwidth .tcpa]}
1

ztk 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.

ttk::checkbutton with -text Agree -variable ::ref::off and -state normal-state normalttk::checkbutton with -text Agree -variable ::ref::off and -state disabled-state disabled
Each with -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
normal

ztk 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.

ttk::checkbutton with -text Agree -variable ::ref::off and -style TCheckbutton-style TCheckbuttonttk::checkbutton with -text Agree -variable ::ref::off and -style Switch.TCheckbutton-style Switch.TCheckbutton
Each with -text Agree -variable ::ref::off.
% ttk::style configure Accent.TCheckbutton -foreground white
> ttk::checkbutton .tcsy -style Accent.TCheckbutton
> .tcsy cget -style
Accent.TCheckbutton

ztk 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
0

ztk 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.

ttk::checkbutton with -variable ::ref::off and -text Agree-text Agreettk::checkbutton with -variable ::ref::off and -text {Send me the news}-text {Send me the news}
Each with -variable ::ref::off.
% ttk::checkbutton .tctx -text Save
> set w [winfo reqwidth .tctx]
> .tctx configure -text "Save as"
> expr {[winfo reqwidth .tctx] > $w}
1

Each 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}
1

Each 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
selected

Each 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]}
1

ztk stores each value at configure, as Tk 8.6.16 does.

Bindings

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

EventDescription
<<Invoke>>The same as the space key. event generate .c <<Invoke>> sends it.

States

StateDescription
selectedThe variable holds the -onvalue: the theme draws a check mark.
alternateThe variable does not exist, or holds an empty string: the theme draws a dash, the mixed state.
activeThe pointer is over the button. hover is the same flag in ztk.
pressedButton 1 is down on the button.
focusThe button has the keyboard focus.
disabledThe button changes no value, and the theme draws it grayed.
ttk::checkbutton with -text Agree -variable ::ref::off and no state flagno state flagttk::checkbutton with -text Agree -variable ::ref::off and state selectedstate selectedttk::checkbutton with -text Agree -variable ::ref::off and state alternatestate alternatettk::checkbutton with -text Agree -variable ::ref::off and state activestate activettk::checkbutton with -text Agree -variable ::ref::off and state focusstate focusttk::checkbutton with -text Agree -variable ::ref::off and state disabledstate disabledttk::checkbutton with -text Agree -variable ::ref::off and state selected disabledstate selected disabled
Each with -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

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztk 1.0.0-beta.1

See also