beetlebug▾
/

ttk::button

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

CommandDescription
ttk::button invokeRuns the command of the button, as a click does.
ttk::button stateGives the state flags of the widget, or turns flags on and off.
ttk::button instateTests the state flags of the widget, and runs a script when they match.
ttk::button identifyGives the name of the element of the widget at a point.
ttk::button cgetGives the value of an option of the widget.
ttk::button 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 a click or invoke runs.
-compoundcompoundemptycompoundWhich of the text and the image show, and the place of the image.
-cursorcursor nameemptycursorThe shape of the pointer over the widget.
-defaultstatenormaldefaultMarks the default button of a dialog.
-imageimage listemptyimageAn image to show, with other images for some states.
-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 TButton or a style of the script such as Accent.TButton.
-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.
-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 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.

ttk::button with -text Save -image ::ref::icon and -compound none-compound nonettk::button with -text Save -image ::ref::icon and -compound text-compound textttk::button with -text Save -image ::ref::icon and -compound image-compound imagettk::button with -text Save -image ::ref::icon and -compound center-compound centerttk::button with -text Save -image ::ref::icon and -compound top-compound topttk::button with -text Save -image ::ref::icon and -compound bottom-compound bottomttk::button with -text Save -image ::ref::icon and -compound left-compound leftttk::button with -text Save -image ::ref::icon and -compound right-compound right
Each with -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.

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
hand2

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

ttk::button with -text Save -compound left and -image {}-image {}ttk::button with -text Save -compound left and -image ::ref::icon-image ::ref::icon
Each with -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.

ttk::button with -text Save and -padding {}-padding {}ttk::button with -text Save and -padding 2-padding 2ttk::button with -text Save and -padding {16 4}-padding {16 4}ttk::button with -text Save and -padding {24 12}-padding {24 12}
Each with -text Save.
% ttk::button .tbpa -text Hi
> ttk::button .tbpa30 -text Hi -padding 30
> expr {[winfo reqwidth .tbpa30] > [winfo reqwidth .tbpa]}
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::button with -text Save and -state normal-state normalttk::button with -text Save and -state disabled-state disabled
Each with -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
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 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.

ttk::button with -text Save and -style TButton-style TButtonttk::button with -text Save and -style Primary.TButton-style Primary.TButton
Each with -text Save.
% ttk::style configure Accent.TButton -foreground white
> ttk::button .tbsy -style Accent.TButton
> .tbsy cget -style
Accent.TButton

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::button .tbtf -takefocus 0
> .tbtf 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::button with -text Save-text Savettk::button with -text {Save all files}-text {Save all files}
% ttk::button .tbtx -text Save
> set w [winfo reqwidth .tbtx]
> .tbtx configure -text "Save as"
> expr {[winfo reqwidth .tbtx] > $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 tbtitle Open
> ttk::button .tbtv -textvariable tbtitle
> set w [winfo reqwidth .tbtv]
> set tbtitle "Open a file"
> expr {[winfo reqwidth .tbtv] > $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::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.

ttk::button with -text Save and -width {}-width {}ttk::button with -text Save and -width 4-width 4ttk::button with -text Save and -width 12-width 12
Each with -text Save.
% ttk::button .tbwi -text Hi
> ttk::button .tbwi10 -text Hi -width 10
> expr {[winfo reqwidth .tbwi10] > [winfo reqwidth .tbwi]}
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. 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

EventDescription
<<Invoke>>Runs the -command script at once, as the space key does. event generate .b <<Invoke>> sends it.

States

StateDescription
activeThe pointer is over the button. hover is the same flag in ztk.
pressedThe button is pressed.
focusThe button has the keyboard focus.
disabledThe button runs no script, and the theme draws it grayed.
ttk::button with -text Save and no state flagno state flagttk::button with -text Save and state activestate activettk::button with -text Save and state pressedstate pressedttk::button with -text Save and state focusstate focusttk::button with -text Save and state disabledstate disabled
Each with -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

Facts

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

See also