beetlebug▾
/

ttk::spinbox

Makes a themed spinbox: an entry field with arrows that step a number or a value of a list.

ttk::spinbox PATHNAME ?OPTION VALUE ...?

Description

Makes a themed spinbox with the name PATHNAME and the options of the call, and makes the command PATHNAME for the widget commands below. The spinbox is a ttk::entry with an up arrow and a down arrow. A click on an arrow, or the virtual events <<Increment>> and <<Decrement>>, steps the value and runs -command.

With -values, a step moves to the next or the previous value of the list. With no -values, a step adds -increment to the number or subtracts it, in the range of -from and -to. A step from an empty field gives the first value. -wrap goes from one end of the range to the other.

The parent of PATHNAME must exist, and PATHNAME must not. The class of the spinbox is TSpinbox, the name of its style. The spinbox has each option and each widget command of ttk::entry, and adds the options of the range and set.

Widget commands

CommandDescription
ttk::spinbox bboxGives the box of a character of the text.
ttk::spinbox deleteDeletes characters of the text.
ttk::spinbox getGives the text of the field.
ttk::spinbox icursorMoves the insert cursor before a character.
ttk::spinbox indexGives the numeric index of an index of the field.
ttk::spinbox insertInserts a text before a character.
ttk::spinbox selectionGives, sets, or clears the selected text of the field.
ttk::spinbox setSets the text of the field.
ttk::spinbox validateRuns the validation script now and gives its result.
ttk::spinbox xviewGives or moves the visible part of a text longer than the field.
ttk::spinbox stateGives the state flags of the widget, or turns flags on and off.
ttk::spinbox instateTests the state flags of the widget, and runs a script when they match.
ttk::spinbox identifyGives the name of the element of the widget at a point.
ttk::spinbox cgetGives the value of an option of the widget.
ttk::spinbox configureGives or sets the options of the widget.

Options

OptionTypeDefaultDatabase nameDescription
-backgroundcoloremptywindowColorThe color of the field behind the text, in place of the color of the style.
-classclass nameemptyThe class of the widget for the option database and for the bindings, set only at creation.
-commandscriptemptycommandThe script that runs after each step of the value.
-cursorcursor nameemptycursorThe shape of the pointer over the widget.
-exportselectionboolean1exportSelectionWhether the selected text is also the selection of the display for other widgets and programs.
-fontfontTkTextFontfontThe font of the text.
-foregroundcoloremptytextColorThe color of the text, in place of the color of the style.
-formatformat stringemptyformatThe format of a number after a step, in the form %W.Pf.
-fromdouble0fromThe lowest number of the range.
-incrementdouble1incrementThe size of one step of the number.
-invalidcommandscriptemptyinvalidCommandThe script that runs when the validation script gives false.
-justifyjustificationleftjustifyThe side of the field that the text lines up with, when the text is shorter than the field.
-placeholderstringemptyplaceHolderA hint text that shows while the field is empty.
-placeholderforegroundcolor#b3b3b3placeholderForegroundThe color of the hint text of -placeholder.
-showstringemptyshowA character that shows in place of each character of the text, such as for a password.
-statestringnormalstateWhether the user can change the text: normal, readonly, or disabled.
-stylestyle nameemptystyleThe style of the widget, such as TSpinbox or a style of the script such as Accent.TSpinbox.
-takefocusboolean, empty, or scriptttk::takefocustakeFocusWhether the Tab key can move the focus to the widget.
-textvariablevariable nameemptytextVariableA global variable that holds the text of the field.
-todouble0toThe highest number of the range.
-validatemodenonevalidateThe events that run the validation script of -validatecommand.
-validatecommandscriptemptyvalidateCommandThe validation script, which gives true for valid text.
-valueslistemptyvaluesA list of values to step through in place of a range of numbers.
-widthinteger20widthThe width of the field in average characters of the font.
-wrapboolean0wrapWhether a step past one end of the range goes to the other end.
-xscrollcommandscriptemptyxScrollCommandThe command prefix that the field calls when its view changes, for a scrollbar.

-background COLOR

The color of the field behind the text, in place of the color of the style.

A color is a name such as white, or #RGB, #RRGGBB, or #RRRRGGGGBBBB. An empty value uses the field color of the style.

ttk::spinbox with -textvariable ::ref::name and -background {}-background {}ttk::spinbox with -textvariable ::ref::name and -background {#fde68a}-background {#fde68a}
Each with -textvariable ::ref::name.
% ttk::spinbox .tsbbg -background lightyellow
> .tsbbg cget -background
lightyellow
% ttk::spinbox .tsbbg2 -background nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor

-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 TSpinbox, 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::spinbox .tsbcl -class Fancy
> winfo class .tsbcl
Fancy

% ttk::spinbox .tsbcl3
> winfo class .tsbcl3
TSpinbox
% ttk::spinbox .tsbcl2
> .tsbcl2 configure -class Other
attempt to change read-only option
-errorcode NONE

-command SCRIPT

The script that runs after each step of the value.

The script runs at the global level after a click on an arrow, or <<Increment>> or <<Decrement>>, changes the value. A change by typing or by set runs no script.

% ttk::spinbox .tsbcm -from 0 -to 5 -command {lappend ::tsblog [.tsbcm get]}
> .tsbcm set 1
> event generate .tsbcm <<Increment>>
> set ::tsblog
2

Each string is a valid script.

-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::spinbox .tsbcu -cursor hand2
> .tsbcu cget -cursor
hand2

ztk stores each value of the option with no check.

-exportselection BOOLEAN

Whether the selected text is also the selection of the display for other widgets and programs.

With a true value, a selection in the widget becomes the PRIMARY selection, so the selection get command gives it. With a false value, the selection stays inside the widget.

% ttk::spinbox .tsbex -exportselection 0
> .tsbex cget -exportselection
0
% ttk::spinbox .tsbex2 -exportselection x
expected boolean value but got "x"
-errorcode TCL VALUE NUMBER

-font FONT

The font of the text.

A font name, such as TkTextFont, or a font description, such as {Helvetica 12 bold}. A larger font makes a taller widget, and -width counts characters of this font.

ttk::spinbox with -textvariable ::ref::name and -font TkDefaultFont-font TkDefaultFontttk::spinbox with -textvariable ::ref::name and -font TkFixedFont-font TkFixedFontttk::spinbox with -textvariable ::ref::name and -font {Helvetica 16 bold}-font {Helvetica 16 bold}
Each with -textvariable ::ref::name.
% ttk::spinbox .tsbfo
> ttk::spinbox .tsbfo2 -font {Helvetica 24}
> expr {[winfo reqheight .tsbfo2] > [winfo reqheight .tsbfo]}
1
% ttk::spinbox .tsbfo3 -font {Helvetica 12 weird}
unknown font style "weird"
-errorcode TK LOOKUP FONT_STYLE weird

-foreground COLOR

The color of the text, in place of the color of the style.

The color of the text. An empty value uses the color of the style.

ttk::spinbox with -textvariable ::ref::name and -foreground {}-foreground {}ttk::spinbox with -textvariable ::ref::name and -foreground {#b91c1c}-foreground {#b91c1c}ttk::spinbox with -textvariable ::ref::name and -foreground {#2563eb}-foreground {#2563eb}
Each with -textvariable ::ref::name.
% ttk::spinbox .tsbfg -foreground #123456
> .tsbfg cget -foreground
#123456
% ttk::spinbox .tsbfg2 -foreground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor

-format FORMAT

The format of a number after a step, in the form %W.Pf.

A step writes the new number with this format of format, such as %.2f for two digits after the point. An empty value writes the number in its shortest form.

% ttk::spinbox .tsbfm -from 0 -to 1 -increment 0.25 -format %.2f
> .tsbfm set 0
> event generate .tsbfm <<Increment>>
> .tsbfm get
0.25
% ttk::spinbox .tsbfm2 -format bogus
bad spinbox format specifier "bogus"
-errorcode NONE

-from NUMBER

The lowest number of the range.

A step down stops at this number, or goes to -to with -wrap. A step from an empty field gives this number.

% ttk::spinbox .tsbfr -from 1 -to 9
> event generate .tsbfr <<Increment>>
> .tsbfr get
1
% ttk::spinbox .tsbfr2 -from x
expected floating-point number but got "x"
-errorcode TCL VALUE NUMBER

-increment NUMBER

The size of one step of the number.

A step up adds this number, and a step down subtracts it.

% ttk::spinbox .tsbin -from 0 -to 100 -increment 10
> .tsbin set 50
> event generate .tsbin <<Decrement>>
> .tsbin get
40
% ttk::spinbox .tsbin2 -increment x
expected floating-point number but got "x"
-errorcode TCL VALUE NUMBER

-invalidcommand SCRIPT

The script that runs when the validation script gives false.

The script runs at the global level after -validatecommand gives false. It has the same % substitutions as -validatecommand. A common use is bell.

% ttk::spinbox .tsbic -validate key -validatecommand {string is digit %P} -invalidcommand {set ::tsbbad %S}
> .tsbic insert 0 x
> set ::tsbbad
x

Each string is a valid script.

-justify SIDE

The side of the field that the text lines up with, when the text is shorter than the field.

left puts the text at the left edge, right at the right edge, and center in the middle.

Values: left, center, right.

% ttk::spinbox .tsbju -justify right
> .tsbju cget -justify
right
% ttk::spinbox .tsbju2 -justify middle
bad justification "middle": must be left, right, or center
-errorcode TCL LOOKUP INDEX justification middle

-placeholder TEXT

A hint text that shows while the field is empty.

The field shows the text in the color of -placeholderforeground while it has no text. The hint is not part of the value: get gives an empty string.

ttk::spinbox with -textvariable ::ref::empty and -placeholder {}-placeholder {}ttk::spinbox with -textvariable ::ref::empty and -placeholder {Your name}-placeholder {Your name}
Each with -textvariable ::ref::empty.
% ttk::spinbox .tsbph -placeholder Name
> list [.tsbph get] [.tsbph cget -placeholder]
{} Name

Each string is a valid text.

-placeholderforeground COLOR

The color of the hint text of -placeholder.

The color of the text of -placeholder while the field is empty.

ttk::spinbox with -textvariable ::ref::empty -placeholder Your name and -placeholderforeground {#b91c1c}-placeholderforeground {#b91c1c}
Each with -textvariable ::ref::empty -placeholder Your name.
% ttk::spinbox .tsbpf -placeholderforeground gray50
> .tsbpf cget -placeholderforeground
gray50
% ttk::spinbox .tsbpf2 -placeholderforeground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor

-show CHAR

A character that shows in place of each character of the text, such as for a password.

With a value, the field shows the first character of the value once for each character of the text. get gives the real text. An empty value shows the text.

ttk::spinbox with -textvariable ::ref::name and -show {}-show {}ttk::spinbox with -textvariable ::ref::name and -show *-show *
Each with -textvariable ::ref::name.
% ttk::spinbox .tsbsh -show *
> .tsbsh insert 0 secret
> .tsbsh get
secret

Each string is a valid value.

-state STATE

Whether the user can change the text: normal, readonly, or disabled.

readonly sets the state flag readonly: the user can select and copy the text, and insert and delete make no change. disabled sets the flag disabled: the widget ignores the keyboard and the pointer and has the disabled look. normal clears the two flags. ztk stores each other value with no check.

ValueDescription
normalThe text can change.
readonlyThe text is fixed, and the user can select it.
disabledThe text is fixed, and the widget ignores the keyboard and the pointer.
ttk::spinbox with -textvariable ::ref::name and -state normal-state normalttk::spinbox with -textvariable ::ref::name and -state disabled-state disabled
Each with -textvariable ::ref::name.
% ttk::spinbox .tsbst -state readonly
> .tsbst insert 0 abc
> list [.tsbst get] [.tsbst state]
{} readonly

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 TSpinbox or a style of the script such as Accent.TSpinbox.

An empty value uses the style TSpinbox. A style name of the script ends with the name of the base style, such as Accent.TSpinbox, and ttk::style configure and ttk::style map give it its options. A style with no settings looks like its base style.

% ttk::style configure Accent.TSpinbox -foreground white
> ttk::spinbox .tsbsy -style Accent.TSpinbox
> .tsbsy cget -style
Accent.TSpinbox

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::spinbox .tsbtf -takefocus 0
> .tsbtf cget -takefocus
0

ztk stores each value of the option with no check.

-textvariable VAR

A global variable that holds the text of the field.

The field shows the value of the global variable VAR. A change of the text sets the variable, and a change of the variable changes the text.

% set ::tsbname Ada
> ttk::spinbox .tsbtv -textvariable ::tsbname
> .tsbtv insert end " Lovelace"
> set ::tsbname
Ada Lovelace

Each name is a valid variable name.

-to NUMBER

The highest number of the range.

A step up stops at this number, or goes to -from with -wrap.

% ttk::spinbox .tsbto -from 0 -to 10
> .tsbto set 10
> event generate .tsbto <<Increment>>
> .tsbto get
10
% ttk::spinbox .tsbto2 -to x
expected floating-point number but got "x"
-errorcode TCL VALUE NUMBER

-validate MODE

The events that run the validation script of -validatecommand.

The script of -validatecommand runs at each event of the mode. When the script gives false for a change of the text by a key, the change does not happen. An error of the script sets the mode to none.

ValueDescription
noneNo validation.
keyA change of the text by a key, insert, or delete.
focusThe widget gets or loses the focus.
focusinThe widget gets the focus.
focusoutThe widget loses the focus.
allEach of the events above.
% ttk::spinbox .tsbva -validate key -validatecommand {string is digit %P}
> .tsbva insert 0 12
> .tsbva insert end a
> .tsbva get
12
% ttk::spinbox .tsbva2 -validate bogus
bad validate "bogus": must be all, key, focus, focusin, focusout, or none
-errorcode TCL LOOKUP INDEX validate bogus

-validatecommand SCRIPT

The validation script, which gives true for valid text.

Before the script runs, each % code in it changes to a value: %d is 1 for an insert, 0 for a delete, and -1 for other events. %i is the index of the change, %P the text after the change, %s the text before it, and %S the text of the insert or the delete. %v is the mode of -validate, %V the event (key, focusin, focusout, or forced), and %W the path of the widget.

% ttk::spinbox .tsbvc -validate key -validatecommand {set ::tsbargs [list %d %i %P %s %S %V]; expr 1}
> .tsbvc insert 0 ab
> set ::tsbargs
1 0 ab {} ab key

Each string is a valid script.

-values LIST

A list of values to step through in place of a range of numbers.

With a list, a step goes to the next or the previous element, and -from, -to, and -increment have no effect.

% ttk::spinbox .tsbvl -values {low mid high}
> .tsbvl set mid
> event generate .tsbvl <<Increment>>
> .tsbvl get
high

ztk stores each value at configure, as Tk 8.6.16 does.

-width CHARS

The width of the field in average characters of the font.

The field asks for room for this count of characters. A longer text scrolls inside the field.

ttk::spinbox with -textvariable ::ref::name and -width 8-width 8ttk::spinbox with -textvariable ::ref::name and -width 20-width 20ttk::spinbox with -textvariable ::ref::name and -width 30-width 30
Each with -textvariable ::ref::name.
% ttk::spinbox .tsbwi -width 10
> ttk::spinbox .tsbwi30 -width 30
> expr {[winfo reqwidth .tsbwi30] > [winfo reqwidth .tsbwi]}
1
% ttk::spinbox .tsbwi2 -width x
expected integer but got "x"
-errorcode TCL VALUE NUMBER

-wrap BOOLEAN

Whether a step past one end of the range goes to the other end.

With a true value, a step up from the last value gives the first value, and a step down from the first value gives the last value.

% ttk::spinbox .tsbwr -from 0 -to 10 -wrap 1
> .tsbwr set 10
> event generate .tsbwr <<Increment>>
> .tsbwr get
0
% ttk::spinbox .tsbwr2 -wrap x
expected boolean value but got "x"
-errorcode TCL VALUE NUMBER

-xscrollcommand PREFIX

The command prefix that the field calls when its view changes, for a scrollbar.

When the visible part of the text changes, the field calls the prefix with two fractions appended: the start and the end of the visible part. Give it the set command of a horizontal scrollbar. The prefix also runs when the text changes.

% ttk::spinbox .tsbxs -width 10 -xscrollcommand {lappend ::tsbview}
> pack .tsbxs
> .tsbxs insert 0 [string repeat abc 50]
> update
> lassign [lrange $::tsbview end-1 end] first last
> expr {$last - $first < 1}
1

Each string is a valid prefix.

Bindings

EventDescription
<Key>Inserts the character of the key at the insert cursor, in place of the selection. Tab, Return, Escape, and the keys with Control or Alt insert no text.
<Key-BackSpace>Deletes the selection, or the character before the insert cursor.
<Key-Delete>Deletes the selection, or the character after the insert cursor.
<Key-Left>Moves the insert cursor one character to the left and clears the selection. With Shift, the selection grows or shrinks to the new place.
<Key-Right>Moves the insert cursor one character to the right and clears the selection. With Shift, the selection grows or shrinks to the new place.
<Control-Key-Left>Moves the insert cursor to the start of the word before it.
<Control-Key-Right>Moves the insert cursor to the end of the word after it.
<Key-Home>Moves the insert cursor to the start of the text. With Shift, the selection grows to the start. Control-a does the same as Home.
<Key-End>Moves the insert cursor to the end of the text. With Shift, the selection grows to the end. Control-e does the same as End.
<Button-1>Places the insert cursor at the pointer, clears the selection, and gives the widget the focus.
<B1-Motion>Selects the text from the place of the press to the pointer, and scrolls the text when the pointer is past an end.
<Shift-Button-1>The selection grows or shrinks to the pointer.
<Double-Button-1>Selects the word under the pointer.
<Triple-Button-1>Selects all of the text.
<Shift-MouseWheel>Scrolls the text to the left or to the right.
<FocusIn>The insert cursor shows and blinks.
<FocusOut>The insert cursor goes.
<Control-Key-c>Sends <<Copy>>.
<Control-Key-x>Sends <<Cut>>.
<Control-Key-v>Sends <<Paste>>.
<Control-Key-z>Sends <<Undo>>.
<Control-Key-y>Sends <<Redo>>. Control-Shift-z does the same.
<Control-Key-slash>Sends <<SelectAll>>.
<Control-Key-backslash>Sends <<SelectNone>>.
<Key-Up>Sends <<Increment>>.
<Key-Down>Sends <<Decrement>>.

Virtual events

EventDescription
<<Copy>>Puts the selected text on the clipboard. With -show, the clipboard gets no text.
<<Cut>>Puts the selected text on the clipboard and deletes it.
<<Paste>>Inserts the text of the clipboard at the insert cursor, in place of the selection.
<<Clear>>Deletes the selected text.
<<Undo>>Undoes the last edit.
<<Redo>>Makes the last edit that <<Undo>> undid again.
<<SelectAll>>Selects all of the text. event generate .s <<SelectAll>> sends it.
<<SelectNone>>Clears the selection.
<<Increment>>Sets the next value of -values, or the value plus -increment, and runs the -command script.
<<Decrement>>Sets the value before in -values, or the value minus -increment, and runs the -command script.

States

StateDescription
focusThe widget has the keyboard focus: the insert cursor shows, and the theme draws the focus ring.
disabledThe widget ignores the keyboard and the pointer, and the theme draws it grayed.
readonlyThe text can be selected and copied, and no key changes it.
invalidA script turns it on with state invalid, as the result of a check of the value.
hoverThe pointer is over the widget.
ttk::spinbox with -textvariable ::ref::name and no state flagno state flagttk::spinbox with -textvariable ::ref::name and state focusstate focusttk::spinbox with -textvariable ::ref::name and state disabledstate disabled
Each with -textvariable ::ref::name.

Returns

PATHNAME.

Examples

% ttk::spinbox .count -from 0 -to 10 -increment 2
> .count set 4
> event generate .count <<Increment>>
> .count get
6

% ttk::spinbox .day -values {Mon Tue Wed}
> .day set Mon
> event generate .day <<Increment>>
> .day get
Tue

% winfo class [ttk::spinbox .n]
TSpinbox

Errors

% ttk::spinbox .x -foo a
unknown option "-foo"
-errorcode TK LOOKUP OPTION -foo

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

# The parent .no does not exist.
% ttk::spinbox .no.s
bad window path name ".no"
-errorcode TK LOOKUP WINDOW .no

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