ttk::button
Guide: Themed widgets (ttk)
Makes a themed push button: a widget with a text or an image that runs a script when the user clicks it.
ttk::button PATHNAME ?OPTION VALUE ...?
Description
Makes a themed button with the name PATHNAME and the options of the call, and makes the command PATHNAME for the widget commands below. A click on the button runs the script of -command. The button shows its -text, the value of its -textvariable, or its -image.
The parent of PATHNAME must exist, and PATHNAME must not. The class of the button is TButton, the name of its style. In ztk the button uses the code and the CSS rules of the classic button, so one stylesheet styles both. ttk::style changes the look of a style, and -style picks the style of one button.
The button has state flags in place of the colors of a classic button. state turns a flag on or off, and instate tests the flags. A button with the flag disabled runs no script.
Widget commands
| Command | Description |
|---|---|
ttk::button invoke | Runs the command of the button, as a click does. |
ttk::button state | Gives the state flags of the widget, or turns flags on and off. |
ttk::button instate | Tests the state flags of the widget, and runs a script when they match. |
ttk::button identify | Gives the name of the element of the widget at a point. |
ttk::button cget | Gives the value of an option of the widget. |
ttk::button 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 a click or invoke runs. |
-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. |
-default | state | normal | default | Marks the default button of a dialog. |
-image | image list | empty | image | An image to show, with other images for some states. |
-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 TButton or a style of the script such as Accent.TButton. |
-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. |
-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 TButton, 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::button .tbcl -class Fancy
> winfo class .tbcl
Fancy
% ttk::button .tbcl3
> winfo class .tbcl3
TButton% ttk::button .tbcl2
> .tbcl2 configure -class Other
attempt to change read-only option
-errorcode NONE-command SCRIPT
The script that a click or invoke runs.
The script runs at the global level when the user clicks the button, and when a script calls invoke. Its result is the result of invoke, and an error of the script is the error of invoke. A button with the state flag disabled runs no script.
% set n 0
> ttk::button .tbcm -command {incr ::n}
> .tbcm invoke
> .tbcm invoke
> set n
2% ttk::button .tbcm2 -command {error boom}
> .tbcm2 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.

-compound none
-compound text
-compound image
-compound center
-compound top
-compound bottom
-compound left
-compound right-text Save -image ::ref::icon. none shows the image when the widget has one, as image does, and as in Tk.% image create photo tbpic -width 40 -height 20
> ttk::button .tbcpi -image tbpic -text Save
> ttk::button .tbcpl -image tbpic -text Save -compound left
> expr {[winfo reqwidth .tbcpl] > [winfo reqwidth .tbcpi]}
1% ttk::button .tbco -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::button .tbcu -cursor hand2
> .tbcu cget -cursor
hand2ztk stores each value of the option with no check.
-default STATE
Marks the default button of a dialog.
active marks the button as the default of its dialog, the one that the Return key of the dialog runs. normal is a button that can be the default, and disabled is a button that never is. The script binds the Return key itself. The value has no effect on the size of the button.
Values: normal, active, disabled.
% ttk::button .tbde -text OK
> ttk::button .tbde2 -text OK -default active
> expr {[winfo reqwidth .tbde] == [winfo reqwidth .tbde2]}
1% ttk::button .tbde3 -default maybe
bad default "maybe": must be normal, active, or disabled
-errorcode TCL LOOKUP INDEX default maybe-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 {}
-image ::ref::icon-text Save -compound left. ::ref::icon is a photo image of 16 by 16 pixels.# The text does not show, so the two widgets have the same width.
% image create photo tbicon -width 40 -height 20
> ttk::button .tbim -image tbicon -text Save
> ttk::button .tbim2 -image tbicon
> expr {[winfo reqwidth .tbim] == [winfo reqwidth .tbim2]}
1% ttk::button .tbim3 -image noimage
image "noimage" doesn't exist
-errorcode TK LOOKUP IMAGE noimage-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 2
-padding {16 4}
-padding {24 12}-text Save.% ttk::button .tbpa -text Hi
> ttk::button .tbpa30 -text Hi -padding 30
> expr {[winfo reqwidth .tbpa30] > [winfo reqwidth .tbpa]}
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 Save.% ttk::button .tbst -state disabled
> .tbst instate disabled
1
# The `state` command does not change the option.
% ttk::button .tbst2
> .tbst2 state disabled
> .tbst2 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 TButton or a style of the script such as Accent.TButton.
An empty value uses the style TButton. A style name of the script ends with the name of the base style, such as Accent.TButton, and ttk::style configure and ttk::style map give it its options. A style with no settings looks like its base style.

-style TButton
-style Primary.TButton-text Save.% ttk::style configure Accent.TButton -foreground white
> ttk::button .tbsy -style Accent.TButton
> .tbsy cget -style
Accent.TButtonztk 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::button .tbtf -takefocus 0
> .tbtf 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 Save
-text {Save all files}% ttk::button .tbtx -text Save
> set w [winfo reqwidth .tbtx]
> .tbtx configure -text "Save as"
> expr {[winfo reqwidth .tbtx] > $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 tbtitle Open
> ttk::button .tbtv -textvariable tbtitle
> set w [winfo reqwidth .tbtv]
> set tbtitle "Open a file"
> expr {[winfo reqwidth .tbtv] > $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::button .tbun -text Save -underline 0
> .tbun cget -underline
0% ttk::button .tbun2 -underline x
expected integer but got "x"
-errorcode TCL VALUE NUMBER-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.

-width {}
-width 4
-width 12-text Save.% ttk::button .tbwi -text Hi
> ttk::button .tbwi10 -text Hi -width 10
> expr {[winfo reqwidth .tbwi10] > [winfo reqwidth .tbwi]}
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. A button that a press armed, and that the pointer left, arms again. |
<Leave> | The pointer leaves the button: a release then runs no script. |
<Button-1> | A press arms the button and gives it the focus. A press on a disabled button does not arm it or give it the focus. |
<ButtonRelease-1> | The release runs the -command script, when the press armed the button and the pointer is still over it. |
<Key-space> | Runs the -command script at once, as invoke does. |
Virtual events
| Event | Description |
|---|---|
<<Invoke>> | Runs the -command script at once, as the space key does. event generate .b <<Invoke>> sends it. |
States
| State | Description |
|---|---|
active | The pointer is over the button. hover is the same flag in ztk. |
pressed | The button is pressed. |
focus | The button has the keyboard focus. |
disabled | The button runs no script, and the theme draws it grayed. |

no state flag
state active
state pressed
state focus
state disabled-text Save.Returns
PATHNAME.
Examples
% ttk::button .save -text Save -command {set ::saved 1}
> .save invoke
> set saved
1
% winfo class [ttk::button .go -text Go]
TButton
Errors
# A themed button has no color options. A style gives the colors.
% ttk::button .x -background red
unknown option "-background"
-errorcode TK LOOKUP OPTION -background
% ttk::button .twice
> ttk::button .twice
window name "twice" already exists in parent
-errorcode TK VALUE WINDOW EXISTS
# The parent .no does not exist.
% ttk::button .no.b
bad window path name ".no"
-errorcode TK LOOKUP WINDOW .no
% ttk::button
wrong # args: should be "ttk::button 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. - The theme of ztk gives a button no minimum width, so a button with a short text is narrower than in Tk 8.6.16.
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztk 1.0.0-beta.1