(new in Tk 8.4) The Spinbox widget is a variant of the standard tkinter Entry widget, which can be used to select from a fixed number of values.
When to use the Spinbox Widget
The Spinbox widget can be used instead of an ordinary Entry, in cases where the user only has a limited number of ordered values to choose from.
Note that the spinbox widget is only available Python 2.3 and later, when linked against Tk 8.4 or later. Also note that several Tk spinbox methods appears to be missing from the tkinter bindings in Python 2.3.
Patterns #
The spinbox behaves pretty much like an ordinary Entry widget. The main difference is that you can specify what values to allow, either as a range, or using a tuple.
from tkinter import * master = Tk() w = Spinbox(master, from_=0, to=10) w.pack() mainloop()
You can specify a set of values instead of a range:
w = Spinbox(values=(1, 2, 4, 8)) w.pack()
Reference #
- Spinbox(master=None, **options) (class) [#]
-
A spinbox widget.
- master
- Parent widget.
- **options
- Widget options. See the description of the config method for a list of available options.
- bbox(index) [#]
-
Returns the bounding box of a given character.
- config(**options) [#]
-
Modifies one or more widget options. If no options are given, the method returns a dictionary containing all current option values.
- **options
- Widget options.
- activebackground=
- Default is system specific. (activeBackground/Background)
- background=
- Default is system specific. (background/Background)
- bd=
- Same as borderwidth.
- bg=
- Same as background.
- borderwidth=
- Widget border width. The default is system specific, but is usually a few pixels. (borderWidth/BorderWidth)
- buttonbackground=
- Button background color. Default is system specific. (Button.background/Background)
- buttoncursor=
- Cursor to use when the mouse pointer is moved over the button part of this widget. No default value. (Button.cursor/Cursor)
- buttondownrelief=
- The border style to use for the up button. Default is RAISED. (Button.relief/Relief)
- buttonuprelief=
- The border style to use for the down button. Default is RAISED. (Button.relief/Relief)
- command=
- A function or method that should be called when a button is pressed. No default value. (command/Command)
- cursor=
- The cursor to use when the mouse pointer is moved over the entry part of this widget. Default is a text insertion cursor (usually XTERM). (cursor/Cursor)
- disabledbackground=
- The background color to use when the widget is disabled. The default is system specific. (disabledBackground/DisabledBackground)
- disabledforeground=
- The text color to use when the widget is disabled. The default is system specific. (disabledForeground/DisabledForeground)
- exportselection=
- Default is True. (exportSelection/ExportSelection)
- fg=
- Same as foreground.
- font=
- The font to use in this widget. Default is system specific. (font/Font)
- foreground=
- Text color. Default is system specific. (foreground/Foreground)
- format=
- Format string. No default value. (format/Format)
- from=
-
The minimum value. Used together with to
to limit the spinbox range.
Note that from is a reserved Python keyword. To use this as a keyword argument, add an underscore (from_). (from/From) - highlightbackground=
- Default is system specific. (highlightBackground/HighlightBackground)
- highlightcolor=
- Default is system specific. (highlightColor/HighlightColor)
- highlightthickness=
- No default value. (highlightThickness/HighlightThickness)
- increment=
- Default is 1.0. (increment/Increment)
- insertbackground=
- Color used for the insertion cursor. (insertBackground/Foreground)
- insertborderwidth=
- Width of the insertion cursor’s border. If this is set to a non-zero value, the cursor is drawn using the RAISED border style. (insertBorderWidth/BorderWidth)
- insertofftime=
- Together with insertontime, this option controls cursor blinking. Both values are given in milliseconds. (insertOffTime/OffTime)
- insertontime=
- See insertofftime. (insertOnTime/OnTime)
- insertwidth=
- Width of the insertion cursor. Usually one or two pixels. (insertWidth/InsertWidth)
- invalidcommand=
- No default value. (invalidCommand/InvalidCommand)
- invcmd=
- Same as invalidcommand.
- justify=
- Default is LEFT. (justify/Justify)
- readonlybackground=
- Default is system specific. (readonlyBackground/ReadonlyBackground)
- relief=
- Default is SUNKEN. (relief/Relief)
- repeatdelay=
- Together with repeatinterval, this option controls button auto-repeat. Both values are given in milliseconds. (repeatDelay/RepeatDelay)
- repeatinterval=
- See repeatdelay. (repeatInterval/RepeatInterval)
- selectbackground=
- Default is system specific. (selectBackground/Foreground)
- selectborderwidth=
- No default value. (selectBorderWidth/BorderWidth)
- selectforeground=
- Default is system specific. (selectForeground/Background)
- state=
- One of NORMAL, DISABLED, or “readonly”. Default is NORMAL. (state/State)
- takefocus=
- Indicates that the user can use the Tab key to move to this widget. Default is an empty string, which means that the entry widget accepts focus only if it has any keyboard bindings (default is on, in other words). (takeFocus/TakeFocus)
- textvariable=
- No default value. (textVariable/Variable)
- to=
- See from. (to/To)
- validate=
- Validation mode. Default is NONE. (validate/Validate)
- validatecommand=
- Validation callback. No default value. (validateCommand/ValidateCommand)
- values=
- A tuple containing valid values for this widget. Overrides from/to/increment. (values/Values)
- vcmd=
- Same as validatecommand.
- width=
- Widget width, in character units. Default is 20. (width/Width)
- wrap=
- If true, the up and down buttons will wrap around. (wrap/Wrap)
- xscrollcommand=
- Used to connect a spinbox field to a horizontal scrollbar. This option should be set to the set method of the corresponding scrollbar. (xScrollCommand/ScrollCommand)
- delete(first, last=None) [#]
-
Deletes one or more characters from the spinbox.
- get() [#]
-
Returns the current contents of the spinbox.
- Returns:
- The widget contents, as a string.
- icursor(index) [#]
-
Moves the insertion cursor to the given index. This also sets the INSERT index.
- index
- Where to move the cursor.
- identify(x, y) [#]
-
Identifies the widget element at the given location.
- Returns:
- One of “none”, “buttondown”, “buttonup”, or “entry”.
- index(index) [#]
-
Gets the numerical position corresponding to the given index.
- index
- An index.
- Returns:
- The corresponding numerical index.
- insert(index, text) [#]
-
Inserts text at the given index. Use insert(INSERT, text) to insert text at the cursor, insert(END, text) to append text to the spinbox.
- index
- Where to insert the text.
- string
- The text to insert.
- invoke(element) [#]
-
Invokes a spinbox button.
- element
- What button to invoke. Must be one of “buttonup” or “buttondown”.
- scan_dragto(x) [#]
-
Sets the scanning anchor for fast horizontal scrolling to the given mouse coordinate.
- x
- Current horizontal mouse position.
- scan_mark(x) [#]
-
Scrolls the widget contents sideways according to the given mouse coordinate. The text is moved 10 times the distance between the scanning anchor and the new position.
- x
- Current horizontal mouse position.
- selection_adjust(index) [#]
-
Adjusts the selection to include also the given character. If index is already selected, do nothing.
- index
- The index.
- selection_clear() [#]
-
Clears the selection.
- selection_element(element=None) [#]
-
Selects an element. If no element is specified, this method returns the current element.