button(3tk)
π button β the manual page for button(3tk)
okay bestie, gather round, because today we're talking about button, a Tk Built-In Command, and honestly? she's the moment. she's the whole reason your little GUI window has anything to click. let's get into it. π
β¨ NAME β¨
button β create and manipulate 'button' action widgets. that's it, that's her job, and she does it flawlessly. π
πΈ SYNOPSIS πΈ
button pathName ?options?
short, sweet, iconic. one command and you've got a whole button. periodt.
π STANDARD OPTIONS π
these are the standard girly options, all inherited from the family:
-activebackground-activeforeground-anchor-background-bitmap-borderwidth-compound-cursor-disabledforeground-font-foreground-highlightbackground-highlightcolor-highlightthickness-image-justify-padx-pady-relief-repeatdelay-repeatinterval-takefocus-text-textvariable-underline-wraplength
π«Ά see the options manual entry for the details on all the standard options β she doesn't gatekeep, she just points you to the right page.
π¦ WIDGET-SPECIFIC OPTIONS π¦
these are her options. the ones that make her, her. π
-commandβ command-line name-command, database namecommand, database classCommand. this is the Tcl command you associate with the button. typically it gets invoked when mouse button 1 is released over the button window. so like, the button's whole personality? this is it.-defaultβ command-line name-default, database namedefault, database classDefault. specifies one of three states for the default ring:normal,active, ordisabled. inactivestate, the button is drawn with the platform specific appearance for a default button. innormalstate, it's drawn with the platform specific appearance for a non-default button, but leaving enough space to draw the default button appearance. (thenormalandactivestates will result in buttons of the same size, so no jump-scare resizing.) indisabledstate, the button is drawn with the non-default button appearance without leaving space for the default appearance β which means thedisabledstate may result in a smaller button than theactivestate. noted. βοΈ-heightβ command-line name-height, database nameheight, database classHeight. your desired height for the button. if an image or bitmap is being displayed in the button then the value is in screen units (i.e. any of the forms acceptable toTk_GetPixels); for text it's in lines of text. if you don't specify this option, the button's desired height is computed from the size of the image or bitmap or text being displayed in it. she'll figure it out, she's smart like that.-overreliefβ command-line name-overrelief, database nameoverRelief, database classOverRelief. an alternative relief for the button, used when the mouse cursor is over the widget. this one is the toolbar button hack: configure-relief flat -overrelief raised. if the value is the empty string, then no alternative relief is used when the cursor is over the button. and the empty string is the default value. π«-stateβ command-line name-state, database namestate, database classState. one of three states:normal,active, ordisabled. innormalstate the button is displayed using the-foregroundand-backgroundoptions.activestate is typically used when the pointer is over the button β then the button is displayed using the-activeforegroundand-activebackgroundoptions.disabledstate means the button is insensitive: the default bindings will refuse to activate the widget and will ignore mouse button presses. in that state,-disabledforegroundand-backgrounddetermine how the button is displayed. she's literally not listening to you. π-widthβ command-line name-width, database namewidth, database classWidth. your desired width. if an image or bitmap is being displayed then the value is in screen units (i.e. any of the forms acceptable toTk_GetPixels). for a text button (no image, or with-compound none) then the width specifies how much space in characters to allocate for the text label. if the width is negative then this specifies a minimum width. if not specified, the button's desired width is computed from the size of the image or bitmap or text being displayed in it. β¨
π DESCRIPTION π
the button command creates a new window (given by the pathName argument) and makes it into a button widget. additional options, described above, may be specified on the command line or in the option database to configure aspects of the button such as its colors, font, text, and initial relief. the button command returns its pathName argument. at the time this command is invoked, there must not exist a window named pathName, but pathName's parent must exist. like, she needs a home, she just doesn't want a roommate with the same name. π
a button is a widget that displays a textual string, bitmap or image. if text is displayed, it must all be in a single font, but it can occupy multiple lines on the screen (if it contains newlines or if wrapping occurs because of the -wraplength option) and one of the characters may optionally be underlined using the -underline option. it can display itself in either of three different ways, according to the -state option; it can be made to appear raised, sunken, or flat; and it can be made to flash. when a user invokes the button (by pressing mouse button 1 with the cursor over the button), then the Tcl command specified in the -command option is invoked. the whole arc. the whole character development. π₯
π WIDGET COMMAND π
the button command creates a new Tcl command whose name is pathName. this command may be used to invoke various operations on the widget. general form:
pathName option ?arg ...?
Option and the args determine the exact behavior of the command. the following commands are possible for button widgets:
pathName cget optionβ returns the current value of the configuration option given by option. option may have any of the values accepted by thebuttoncommand.pathName configure ?option? ?value option value ...?β query or modify the configuration options of the widget. if no option is specified, returns a list describing all of the available options for pathName (seeTk_ConfigureInfofor information on the format of this list). if option is specified with no value, then the command returns a list describing the one named option (this list will be identical to the corresponding sublist of the value returned if no option is specified). if one or more option-value pairs are specified, then the command modifies the given widget option(s) to have the given value(s); in this case the command returns an empty string. option may have any of the values accepted by thebuttoncommand. she contains multitudes.pathName flashβ flash the button. this is accomplished by redisplaying the button several times, alternating between the configuredactivebackgroundandbackgroundcolors. at the end of the flash the button is left in the same normal/active state as when the command was invoked. this command is ignored if the button's state isdisabled. blink blink, she's serving β¨pathName invokeβ invoke the Tcl command associated with the button, if there is one. the return value is the return value from the Tcl command, or an empty string if there is no command associated with the button. this command is ignored if the button's state isdisabled.
π§ DEFAULT BINDINGS π§
Tk automatically creates class bindings for buttons that give them default behavior:
- A button activates whenever the mouse passes over it and deactivates whenever the mouse leaves the button. Under Windows, this binding is only active when mouse button 1 has been pressed over the button.
- A button's relief is changed to
sunkenwhenever mouse button 1 is pressed over the button, and the relief is restored to its original value when button 1 is later released. - If mouse button 1 is pressed over a button and later released over the button, the button is invoked. However, if the mouse is not over the button when button 1 is released, then no invocation occurs.
- When a button has the input focus, the space key causes the button to be invoked.
if the button's state is disabled then none of the above actions occur: the button is completely non-responsive. she is in her silent era and you must respect it. π
the behavior of buttons can be changed by defining new bindings for individual widgets or by redefining the class bindings.
π PLATFORM NOTES π
on Aqua/macOS, some configuration options are ignored for the purpose of drawing of the widget because they would otherwise conflict with platform guidelines. the configure and cget subcommands can still manipulate the values, but do not cause any variation to the look of the widget. the options affected notably include -background and -relief. so like, macOS said "no π
" and honestly we have to live with that.
β EXAMPLES β
this is the classic Tk "Hello, World!" demonstration:
button .b -text "Hello, World!" -command exit
pack .b
this example demonstrates how to handle button accelerators:
button .b1 -text Hello -underline 0
button .b2 -text World -underline 0
bind . <Key-h> {.b1 flash; .b1 invoke}
bind . <Key-w> {.b2 flash; .b2 invoke}
pack .b1 .b2
keyboard shortcuts with a lil flash?? the way that she ate. π₯
π SEE ALSO π
ttk::button(3tk)β her more polished cousin, iykyk
π KEYWORDS π
button, widget
okay so that's the whole page, bestie β every option, every state, every binding, no skips, no gatekeeping, all facts intact. π«Ά
and for the lil sign-off: this one came from Tk, version 4.4, and it's the button(3tk) page. go forth and make buttons. you're gonna slay. πβ¨π
π¦ where this page comes from
- debian trixie: tk8.6-doc 8.6.16-1
- debian trixie: tk9.0-doc 9.0.1-3
- ubuntu questing: tk8.6-doc 8.6.17-1
- ubuntu questing: tk9.0-doc 9.0.2-1
- ubuntu resolute: tk8.6-doc 8.6.17-1build1
- ubuntu resolute: tk9.0-doc 9.0.3-1
- ubuntu stonking: tk8.6-doc 8.6.18-1
- ubuntu stonking: tk9.0-doc 9.0.4-1
- Copyright (c) 1990-1994 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms