button

Makes a push button: a widget with a text or an image that runs a script when the user clicks it.

button PATHNAME ?OPTION VALUE ...?

Description

Makes a button widget 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. A button is a Button in CSS and for the option database, and ztk::class gives it a style class for a stylesheet.

The theme draws the border and the padding, so -borderwidth, -padx, and -pady default to 0, and a value of the script overrides the theme. Some options for the look of Tk on X11 are stored with no effect in the modern theme, such as -highlightthickness, -activebackground, and -overrelief. Differences from Tk lists them, and the theme classic has the look of Tk on X11.

An option needs its full name or a short form of its own, such as -bg for -background: -tex is an unknown option, in ztk and in Tk 8.6.16.

Widget commands

CommandSummary
button invokeRuns the command of the button, as a click does.
button flashFlashes the button between its active and normal colors.
button cgetGives the value of an option of the button.
button configureGives or sets the options of the button.

Options

OptionTypeDefaultDatabase nameSummary
-activebackgroundcolor#ecececactiveBackgroundThe background color under the pointer, in the look of Tk on X11.
-activeforegroundcolor#000000activeForegroundThe text color under the pointer, in the look of Tk on X11.
-anchoranchorcenteranchorThe place of the text or the image in a button larger than it.
-backgroundcolor#d9d9d9backgroundThe background color of the button.
-bitmapbitmap nameemptybitmapA bitmap name of Tk to show in place of the text.
-borderwidthscreen distance0borderWidthThe width of the border, in place of the border of the theme.
-commandscriptemptycommandThe script that a click or invoke runs.
-compoundcompoundnonecompoundShows the image and the text together, with the image at that side.
-cursorcursor nameemptycursorThe shape of the pointer over the button.
-defaultstatedisableddefaultMarks the default button of a dialog.
-disabledforegroundcolor#a3a3a3disabledForegroundThe text color while the state is disabled.
-fontfontTkDefaultFontfontThe font of the text.
-foregroundcolor#000000foregroundThe color of the text.
-heightinteger0heightThe height of the button in lines of text, or in pixels for an image.
-highlightbackgroundcolor#d9d9d9highlightBackgroundThe color of the focus ring while the button has no focus, in the look of Tk on X11.
-highlightcolorcolor#000000highlightColorThe color of the focus ring while the button has the focus, in the look of Tk on X11.
-highlightthicknessscreen distance1highlightThicknessThe width of the focus ring, in the look of Tk on X11.
-imageimage nameemptyimageAn image to show in place of the text.
-justifyjustificationcenterjustifyThe alignment of the lines of a text of more than one line.
-overreliefreliefemptyoverReliefThe relief under the pointer, in the look of Tk on X11.
-padxscreen distance0padXThe space at the left and the right of the content, in place of the padding of the theme.
-padyscreen distance0padYThe space above and below the content, in place of the padding of the theme.
-reliefreliefflatreliefThe 3-D look of the border.
-repeatdelayinteger0repeatDelayThe time in milliseconds before a held button starts to repeat its command.
-repeatintervalinteger0repeatIntervalThe time in milliseconds between two repeats of a held button.
-statestatenormalstateWhether the button works: normal, active, or disabled.
-takefocusboolean, empty, or scriptemptytakeFocusWhether the Tab key can move the focus to the button.
-textstringemptytextThe text of the button.
-textvariablevariable nameemptytextVariableA global variable whose value is the text of the button.
-underlineinteger-1underlineThe index of a character of the text to underline, for a keyboard shortcut.
-widthinteger0widthThe width of the button in characters of the font, or in pixels for an image.
-wraplengthscreen distance0wrapLengthThe longest line of the text before it breaks into a new line.

-activebackground COLOR

The background color under the pointer, in the look of Tk on X11.

The background of the button while the pointer is over it, or while -state is active. A color is a name such as red, or #RGB, #RRGGBB, or #RRRRGGGGBBBB. The modern theme stores the value with no effect.

% button .ab -activebackground #ff0000
> .ab cget -activebackground
#ff0000
% button .ab2 -activebackground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor

-activeforeground COLOR

The text color under the pointer, in the look of Tk on X11.

The color of the text while the pointer is over the button, or while -state is active. The class in the option database is Background, as in Tk.

% button .af -activeforeground white
> .af cget -activeforeground
white
% button .af2 -activeforeground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor

-anchor ANCHOR

The place of the text or the image in a button larger than it.

When the button is larger than its text or image, such as with -width or a geometry manager that stretches it, the anchor puts the content at that side or corner. center puts it in the middle.

Values: n, ne, e, se, s, sw, w, nw, center.

% button .an -text Go -anchor w
> .an cget -anchor
w
% button .an2 -anchor middle
bad anchor "middle": must be n, ne, e, se, s, sw, w, nw, or center
-errorcode TCL LOOKUP INDEX anchor middle

-background COLOR

The background color of the button.

The color behind the text. -bg is the same option. With no value from the script, cget gives the color of the theme, which can differ from the default of the option database: .b configure -background lists both.

% button .bg -bg red
> .bg cget -background
red

# The default of the option database.
% button .bg3
> lindex [.bg3 configure -background] 3
#d9d9d9
% button .bg2 -background nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor

-bitmap BITMAP

A bitmap name of Tk to show in place of the text.

The name of a built-in bitmap of Tk: error, gray12, gray25, gray50, gray75, hourglass, info, questhead, question, or warning. A value that starts with @ names a file. ztk stores the value for cget and shows no bitmap.

% button .bm -bitmap error
> .bm cget -bitmap
error
% button .bm2 -bitmap nobitmap
bitmap "nobitmap" not defined
-errorcode TK LOOKUP BITMAP nobitmap

-borderwidth DISTANCE

The width of the border, in place of the border of the theme.

The width of the border around the button. -bd is the same option. The default 0 leaves the border to the theme, and a value of the script replaces it. A distance is a count of pixels, or a number with c, i, m, or p for centimeters, inches, millimeters, or points.

% button .bw -text Save
> button .bw6 -text Save -borderwidth 6
> expr {[winfo reqwidth .bw6] > [winfo reqwidth .bw]}
1

% button .bd -bd 3
> .bd cget -borderwidth
3
% button .bw2 -borderwidth x
bad screen distance "x"
-errorcode TK VALUE PIXELS

-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 -state disabled runs no script. An empty script runs no command.

% set n 0
> button .cm -command {incr ::n}
> .cm invoke
> .cm invoke
> set n
2

% button .cm3 -command {list a b}
> .cm3 invoke
a b
% button .cm2 -command {error boom}
> .cm2 invoke
boom
-errorcode NONE

-compound PLACE

Shows the image and the text together, with the image at that side.

With none, a button with an -image shows only the image. Each other value shows the image and the text, with the image at that side of the text, or under the text with center.

Values: none, bottom, center, left, right, top.

% image create photo pic -width 40 -height 20
> button .ci -image pic -text Save
> button .cl -image pic -text Save -compound left
> expr {[winfo reqwidth .cl] > [winfo reqwidth .ci]}
1
% button .co -compound sideways
bad compound "sideways": must be bottom, center, left, none, right, or top
-errorcode TCL LOOKUP INDEX compound sideways

-cursor CURSOR

The shape of the pointer over the button.

A cursor name of Tk, such as hand2, as the first word of a list. The names are the names of the cursor font of X and the names of Tk on Windows, such as size_we, in any case. A value that starts with @ names a file. An empty value uses the cursor of the parent.

% button .cu -cursor hand2
> .cu cget -cursor
hand2
% button .cu2 -cursor nocursor
bad cursor spec "nocursor"
-errorcode TK VALUE CURSOR

-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 value has no effect on the size of the button.

Values: active, disabled, normal.

% button .de -text OK -default active
> .de cget -default
active

% button .de2 -text OK
> button .de3 -text OK -default active
> expr {[winfo reqwidth .de2] == [winfo reqwidth .de3]}
1
% button .de4 -default maybe
bad default "maybe": must be active, disabled, or normal
-errorcode TCL LOOKUP INDEX default maybe

-disabledforeground COLOR

The text color while the state is disabled.

The color of the text while -state is disabled.

% button .df -disabledforeground gray50
> .df cget -disabledforeground
gray50
% button .df2 -disabledforeground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor

-font FONT

The font of the text.

A font name, such as TkDefaultFont, or a font description, such as {Helvetica 12 bold} or {-family Helvetica -size 12}. A larger font makes a larger button. A family that does not exist falls back to a font of the system.

% button .fo -text Save
> button .fo2 -text Save -font {Helvetica 24}
> expr {[winfo reqheight .fo2] > [winfo reqheight .fo]}
1

% button .fo3 -font {Helvetica 12 bold}
> .fo3 cget -font
Helvetica 12 bold
% button .fo4 -font {Helvetica 12 weird}
unknown font style "weird"
-errorcode TK LOOKUP FONT_STYLE weird

% button .fo5 -font {Helvetica big}
expected integer but got "big"
-errorcode TCL VALUE NUMBER

% button .fo6 -font {-family Helvetica -weight heavy}
bad -weight value "heavy": must be normal, or bold
-errorcode TK LOOKUP -weight heavy

% button .fo7 -font {}
font "" doesn't exist
-errorcode TK LOOKUP FONT {}

-foreground COLOR

The color of the text.

The color of the text and of a bitmap. -fg is the same option.

% button .fg -fg #123456
> .fg cget -foreground
#123456
% button .fg2 -foreground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor

-height LINES

The height of the button in lines of text, or in pixels for an image.

For a button with text, the height in lines of the font. For a button with an image, the height in pixels. 0 makes the button as tall as its content.

% button .he -text Save
> button .he3 -text Save -height 3
> expr {[winfo reqheight .he3] > 2 * [winfo reqheight .he]}
1
% button .he2 -height x
expected integer but got "x"
-errorcode TCL VALUE NUMBER

-highlightbackground COLOR

The color of the focus ring while the button has no focus, in the look of Tk on X11.

The color of the highlight ring of -highlightthickness while the button does not have the focus.

% button .hb -highlightbackground white
> .hb cget -highlightbackground
white
% button .hb2 -highlightbackground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor

-highlightcolor COLOR

The color of the focus ring while the button has the focus, in the look of Tk on X11.

The color of the highlight ring of -highlightthickness while the button has the focus.

% button .hc -highlightcolor blue
> .hc cget -highlightcolor
blue
% button .hc2 -highlightcolor nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor

-highlightthickness DISTANCE

The width of the focus ring, in the look of Tk on X11.

The width of the ring around the button that shows the focus. The modern theme draws the focus with the stylesheet, so it stores the value with no effect on the size.

% button .ht -text Save
> button .ht5 -text Save -highlightthickness 5
> expr {[winfo reqwidth .ht5] == [winfo reqwidth .ht]}
1
% button .ht2 -highlightthickness x
bad screen distance "x"
-errorcode TK VALUE PIXELS

-image IMAGE

An image to show in place of the text.

The name of an image of image create. With an image and -compound none, the button shows the image and no text. The image must exist when the option is set.

# The text does not show, so the two buttons have the same width.
% image create photo icon -width 40 -height 20
> button .im -image icon -text Save
> button .im2 -image icon
> expr {[winfo reqwidth .im] == [winfo reqwidth .im2]}
1
% button .im3 -image noimage
image "noimage" doesn't exist
-errorcode TK LOOKUP IMAGE noimage

-justify SIDE

The alignment of the lines of a text of more than one line.

How the lines of a text with more than one line line up with each other. A text breaks into lines at each newline and at -wraplength.

Values: left, center, right.

% button .ju -text "one\ntwo" -justify left
> .ju cget -justify
left
% button .ju2 -justify middle
bad justification "middle": must be left, right, or center
-errorcode TCL LOOKUP INDEX justification middle

-overrelief RELIEF

The relief under the pointer, in the look of Tk on X11.

The relief of the button while the pointer is over it. An empty value keeps -relief. The modern theme stores the value with no effect.

% button .or -overrelief raised
> .or cget -overrelief
raised
% button .or2 -overrelief bumpy
bad relief "bumpy": must be flat, groove, raised, ridge, solid, or sunken
-errorcode TCL LOOKUP INDEX relief bumpy

-padx DISTANCE

The space at the left and the right of the content, in place of the padding of the theme.

The default 0 leaves the padding to the theme, and a value of the script replaces the padding of the theme at both sides.

% button .px -text Save
> button .px20 -text Save -padx 20
> expr {[winfo reqwidth .px20] > [winfo reqwidth .px]}
1
% button .px2 -padx x
bad screen distance "x"
-errorcode TK VALUE PIXELS

-pady DISTANCE

The space above and below the content, in place of the padding of the theme.

The default 0 leaves the padding to the theme, and a value of the script replaces the padding of the theme above and below.

% button .py -text Save
> button .py10 -text Save -pady 10
> expr {[winfo reqheight .py10] > [winfo reqheight .py]}
1
% button .py2 -pady x
bad screen distance "x"
-errorcode TK VALUE PIXELS

-relief RELIEF

The 3-D look of the border.

The style of the border: raised and sunken look 3-D, and groove and ridge are lines. The theme draws the border, and the CSS of the theme can give a relief its own look with border-style.

Values: flat, groove, raised, ridge, solid, sunken.

% button .re -text Go -relief groove
> .re cget -relief
groove
% button .re2 -relief bumpy
bad relief "bumpy": must be flat, groove, raised, ridge, solid, or sunken
-errorcode TCL LOOKUP INDEX relief bumpy

-repeatdelay MS

The time in milliseconds before a held button starts to repeat its command.

In Tk, a value above 0 makes a held button run its command again after this time, and then each -repeatinterval. ztk stores the value for cget. The effect on a held button is not checked on this page.

% button .rd -repeatdelay 400
> .rd cget -repeatdelay
400
% button .rd2 -repeatdelay x
expected integer but got "x"
-errorcode TCL VALUE NUMBER

-repeatinterval MS

The time in milliseconds between two repeats of a held button.

The time between two runs of the command of a held button, after -repeatdelay. ztk stores the value for cget. The effect on a held button is not checked on this page.

% button .ri -repeatinterval 100
> .ri cget -repeatinterval
100
% button .ri2 -repeatinterval x
expected integer but got "x"
-errorcode TCL VALUE NUMBER

-state STATE

Whether the button works: normal, active, or disabled.

A disabled button ignores clicks, and invoke on it runs no script and gives an empty string. Set the state back to normal to turn the button on again.

ValueMeaning
normalThe button works and has its normal look.
activeThe button has the look of a button under the pointer.
disabledThe button runs no command, with a click or with invoke, and has the text color of -disabledforeground.
% button .st -state disabled -command {set ::hit 1}
> .st invoke
> info exists ::hit
0

% .st configure -state normal
> .st invoke
1
% button .st2 -state busy
bad state "busy": must be active, disabled, or normal
-errorcode TCL LOOKUP INDEX state busy

-takefocus VALUE

Whether the Tab key can move the focus to the button.

1 puts the button in the order of the Tab key, 0 leaves it out, and an empty value uses the default rule of Tk for the widget. In Tk the value can also be a script, and its result is the answer. ztk stores each value with no check.

% button .tf -takefocus 0
> .tf cget -takefocus
0

ztk stores each value of the option with no check.

-text TEXT

The text of the button.

The text that the button shows. A newline starts a new line. A longer text makes a wider button, unless -width sets the width. With -textvariable, the variable sets the text.

% button .tx -text Save
> set w [winfo reqwidth .tx]
> .tx configure -text "Save as"
> expr {[winfo reqwidth .tx] > $w}
1

Each string is a valid text.

-textvariable VAR

A global variable whose value is the text of the button.

The button shows the value of the global variable VAR, and changes its text when the variable changes. When VAR has no value, the button sets it to its -text. The variable stays after the button is destroyed.

% button .tv -textvariable label
> set label Open
> .tv cget -text
Open

% button .tv2 -textvariable start -text Start
> set start
Start

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.

% button .un -text Save -underline 0
> .un cget -underline
0
% button .un2 -underline x
expected integer but got "x"
-errorcode TCL VALUE NUMBER

-width CHARS

The width of the button in characters of the font, or in pixels for an image.

For a button with text, the width in average characters of the font. For a button with an image, the width in pixels. 0 makes the button as wide as its content.

% button .wi -text Hi
> button .wi10 -text Hi -width 10
> expr {[winfo reqwidth .wi10] > [winfo reqwidth .wi]}
1
% button .wi2 -width x
expected integer but got "x"
-errorcode TCL VALUE NUMBER

-wraplength DISTANCE

The longest line of the text before it breaks into a new line.

A text longer than this distance breaks at a space into more lines. 0 breaks no line, so the text is one line long unless it has newlines.

% button .wr -text "one two three four five six"
> button .wr60 -text "one two three four five six" -wraplength 60
> expr {[winfo reqheight .wr60] > [winfo reqheight .wr] && [winfo reqwidth .wr60] < [winfo reqwidth .wr]}
1
% button .wr2 -wraplength x
bad screen distance "x"
-errorcode TK VALUE PIXELS

Returns

PATHNAME.

Examples

% button .save -text Save -command {set ::saved 1}
> .save invoke
> set saved
1

% winfo class [button .go -text Go]
Button

Errors

% button .x -text a -foo b
unknown option "-foo"
-errorcode TK LOOKUP OPTION -foo

% button .twice
> button .twice
window name "twice" already exists in parent
-errorcode TK VALUE WINDOW EXISTS

# The parent .no does not exist.
% button .no.b
bad window path name ".no"
-errorcode TK LOOKUP WINDOW .no

% button
wrong # args: should be "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