checkbutton(3tk)
π checkbutton β Tk Built-In Commands π
ok so bestie. welcome to checkbutton(3tk), the page where we learn how to make and mess with 'checkbutton' boolean selection widgets. yes it's a long one, we're splitting it, grab a snack π
β¨ NAME β¨
checkbutton β Create and manipulate 'checkbutton' boolean selection widgets
β¨ SYNOPSIS β¨
checkbutton pathName ?options?
β¨ STANDARD OPTIONS β¨
these ones are the shared lil icons, the ones every widget's got in common:
-activebackgroundβ-disabledforegroundβ-padx-activeforegroundβ-fontβ-pady-anchorβ-foregroundβ-relief-backgroundβ-highlightbackgroundβ-takefocus-bitmapβ-highlightcolorβ-text-borderwidthβ-highlightthicknessβ-textvariable-compoundβ-imageβ-underline-cursorβ-justifyβ-wraplength
if you want the tea on any of those, go read the options manual entry, it's got all the deets π
β¨ WIDGET-SPECIFIC OPTIONS β¨
these are the ones that are so checkbutton-coded, nobody else gets them π
-command
- Command-Line Name:
-command - Database Name:
command - Database Class:
Command
Specifies a Tcl command to associate with the button. this command is typically invoked when mouse button 1 is released over the button window. and bestie, important: the button's global variable (that's the -variable option) will be updated before the command gets invoked. so it's updated first, then the command runs. no cap.
-height
- Command-Line Name:
-height - Database Name:
height - Database Class:
Height
Specifies a 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 to Tk_GetPixels); for text it is in lines of text. if this option is not specified, the button's desired height is computed from the size of the image or bitmap or text being displayed in it. it just knows β¨
-indicatoron
- Command-Line Name:
-indicatoron - Database Name:
indicatorOn - Database Class:
IndicatorOn
Specifies whether or not the indicator should be drawn. must be a proper boolean value. if false, the -relief option is ignored and the widget's relief is always sunken if the widget is selected and raised otherwise. so like, the relief is just not taking notes from you anymore π
-offrelief
- Command-Line Name:
-offrelief - Database Name:
offRelief - Database Class:
OffRelief
Specifies the relief for the checkbutton when the indicator is not drawn and the checkbutton is off. the default value is βraisedβ. by setting this option to βflatβ and setting -indicatoron to false and -overrelief to βraisedβ, the effect is achieved of having a flat button that raises on mouse-over and which is depressed when activated. this is the behavior typically exhibited by the Bold, Italic, and Underline checkbuttons on the toolbar of a word-processor, for example. literally icon behavior fr fr π₯
-offvalue
- Command-Line Name:
-offvalue - Database Name:
offValue - Database Class:
Value
Specifies value to store in the button's associated variable whenever this button is deselected. defaults to β0β.
-onvalue
- Command-Line Name:
-onvalue - Database Name:
onValue - Database Class:
Value
Specifies value to store in the button's associated variable whenever this button is selected. defaults to β1β.
-overrelief
- Command-Line Name:
-overrelief - Database Name:
overRelief - Database Class:
OverRelief
Specifies an alternative relief for the checkbutton, to be used when the mouse cursor is over the widget. this option can be used to make toolbar buttons, by configuring -relief flat -overrelief raised. if the value of this option is the empty string, then no alternative relief is used when the mouse cursor is over the checkbutton. the empty string is the default value. so like, hovering does nothing extra by default, that's the vibe πΈ
-selectcolor
- Command-Line Name:
-selectcolor - Database Name:
selectColor - Database Class:
Background
Specifies a background color to use when the button is selected. if indicatorOn is true then the color is used as the background for the indicator regardless of the select state. if indicatorOn is false, this color is used as the background for the entire widget, in place of background or activeBackground, whenever the widget is selected. if specified as an empty string then no special color is used for displaying when the widget is selected.
-selectimage
- Command-Line Name:
-selectimage - Database Name:
selectImage - Database Class:
SelectImage
Specifies an image to display (in place of the -image option) when the checkbutton is selected. this option is ignored unless the -image option has been specified. so no image, no selectimage, that's the deal π€
-state
- Command-Line Name:
-state - Database Name:
state - Database Class:
State
Specifies one of three states for the checkbutton: normal, active, or disabled.
- in normal state the checkbutton is displayed using the
-foregroundand-backgroundoptions. - the active state is typically used when the pointer is over the checkbutton. in active state the checkbutton is displayed using the
-activeforegroundand-activebackgroundoptions. - disabled state means that the checkbutton should be insensitive: the default bindings will refuse to activate the widget and will ignore mouse button presses. in this state the
-disabledforegroundand-backgroundoptions determine how the checkbutton is displayed.
three moods, three fits π
-tristateimage
- Command-Line Name:
-tristateimage - Database Name:
tristateImage - Database Class:
TristateImage
Specifies an image to display (in place of the -image option) when the checkbutton is in tri-state mode. this option is ignored unless the -image option has been specified.
-tristatevalue
- Command-Line Name:
-tristatevalue - Database Name:
tristateValue - Database Class:
Value
Specifies the value that causes the checkbutton to display the multi-value selection, also known as the tri-state mode. defaults to ββ.
-variable
- Command-Line Name:
-variable - Database Name:
variable - Database Class:
Variable
Specifies the name of a global variable to set to indicate whether or not this button is selected. defaults to the name of the button within its parent (i.e. the last element of the button window's path name).
-width
- Command-Line Name:
-width - Database Name:
width - Database Class:
Width
Specifies a desired width 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 to Tk_GetPixels); for text it is in characters. if this option is 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 checkbutton command creates a new window (given by the pathName argument) and makes it into a checkbutton widget. Additional options, described above, may be specified on the command line or in the option database to configure aspects of the checkbutton such as its colors, font, text, and initial relief. The checkbutton 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. so like, the name has to be free but its parent's gotta be there already, that's the rule π«Ά
A checkbutton is a widget that displays a textual string, bitmap or image and a square called an indicator. 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. A checkbutton has all of the behavior of a simple button, including the following:
- it can display itself in either of three different ways, according to the
-stateoption; - it can be made to appear raised, sunken, or flat;
- it can be made to flash;
- and it invokes a Tcl command whenever mouse button 1 is clicked over the checkbutton.
In addition, checkbuttons can be selected. If a checkbutton is selected then the indicator is normally drawn with a selected appearance, and a Tcl variable associated with the checkbutton is set to a particular value (normally 1). The indicator is drawn with a check mark inside. If the checkbutton is not selected, then the indicator is drawn with a deselected appearance, and the associated variable is set to a different value (typically 0). The indicator is drawn without a check mark inside.
In the special case where the variable (if specified) has a value that matches the tristatevalue, the indicator is drawn with a tri-state appearance and is in the tri-state mode indicating mixed or multiple values. (This is used when the check box represents the state of multiple items.) The indicator is drawn in a platform dependent manner. Under Unix and Windows, the background interior of the box is βgrayedβ. Under Mac, the indicator is drawn with a dash mark inside.
By default, the name of the variable associated with a checkbutton is the same as the name used to create the checkbutton. The variable name, and the βonβ, βoffβ and βtristateβ values stored in it, may be modified with options on the command line or in the option database. Configuration options may also be used to modify the way the indicator is displayed (or whether it is displayed at all). By default a checkbutton is configured to select and deselect itself on alternate button clicks. In addition, each checkbutton monitors its associated variable and automatically selects and deselects itself when the variables value changes to and from the button's βonβ, βoffβ and βtristateβ values. it's literally watching its own variable like π so dramatic and we love that for it
bestie this is only part 1 π more parts coming, don't go anywhere π
π« WIDGET COMMAND π«
So when you make a checkbutton, Tk is literally creating a whole new Tcl command for you, and its name is pathName. That's your girl. You use her to do stuff to the widget. General shape of it:
pathName option ?arg ...?
The option and the args decide what actually happens. Here's everything you can do to a checkbutton, bestie:
pathName cget optionβ hands you back the current value of whatever configuration option you name in option. And option can be literally any of the values that thecheckbuttoncommand itself accepts. β¨pathName configure ?option? ?value option value ...?β query or change the widget's config options, two-for-one energy. No option at all? You get a list describing every available option forpathName(peepTk_ConfigureInfoif you wanna know the format of that list, iykyk). Just option with no value? You get a list describing that one named option β and ngl that list is identical to the matching sublist of the big list you'd get with no option. And if you pass one or more option-value pairs, the command modifies the given widget option(s) to hold the given value(s), and in that case it returns an empty string. option can be any of the values thecheckbuttoncommand accepts. πpathName deselectβ unchecks the checkbutton and sets its associated variable to its βoffβ value. bye bye selection π«ΆpathName flashβ makes the checkbutton flash π. It does this by redisplaying the checkbutton a bunch of times, alternating between active and normal colors. When the flash is done, the checkbutton is left in the exact same normal/active state it was in when you called the command. Also β heads up β this command is straight up ignored if the checkbutton's state isdisabled.pathName invokeβ does exactly what would've happened if the user clicked the checkbutton with the mouse: toggles the selection state of the button and invokes the Tcl command associated with the checkbutton, if there even is one. What do you get back? The return value from that Tcl command, or an empty string if there's no command associated with the checkbutton. Same deal tho: ignored if the state isdisabled. π₯pathName selectβ checks the checkbutton and sets its associated variable to its βonβ value. slay. πpathName toggleβ flips the selection state of the button, redisplays it, and modifies its associated variable so it reflects the new state. main character behavior fr fr.
π BINDINGS π
Tk goes ahead and auto-makes class bindings for checkbuttons so they come out the box with this default behavior:
- On Unix systems, a checkbutton activates whenever the mouse passes over it, and deactivates whenever the mouse leaves the checkbutton. On Mac and Windows systems tho, when mouse button 1 is pressed over a checkbutton, the button activates whenever the mouse pointer is inside the button, and deactivates whenever the mouse pointer leaves the button. π
- When mouse button 1 is pressed over a checkbutton, it gets invoked β meaning its selection state toggles and the command associated with the button is invoked, if there is one. π±οΈβ¨
- When a checkbutton has the input focus, the space key causes the checkbutton to be invoked. Under Windows there's extra key bindings on top of that: plus (
+) and equal (=) select the button, and minus (-) deselects the button. β
If the checkbutton's state is disabled then none of the above actions happen β the checkbutton is completely non-responsive. Like, ghosted. π
You can change how checkbuttons behave by defining new bindings for individual widgets, or by redefining the class bindings. Your widget, your rules. π
π EXAMPLE π
Here's a little group of uncoupled checkbuttons, iconic:
labelframe .lbl -text "Steps:"
checkbutton .c1 -text Lights -variable lights
checkbutton .c2 -text Cameras -variable cameras
checkbutton .c3 -text Action! -variable action
pack .c1 .c2 .c3 -in .lbl
pack .lbl
π¦ SEE ALSO π¦
button(3tk)options(3tk)radiobutton(3tk)ttk::checkbutton(3tk)
π KEYWORDS π
checkbutton, widget
and that's the whole page, bestie!! this one's from the Tk era, version 4.4, and it's checkbutton(3tk) β go forth and check those buttons, you absolute legend π«Άβ¨πΈ
π¦ where this page comes from
- debian trixie: tk9.0-doc 9.0.1-3
- ubuntu questing: tk9.0-doc 9.0.2-1
- ubuntu resolute: tk9.0-doc 9.0.3-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