(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.
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()
- Spinbox(master=None, **options) (class) [#]
A spinbox widget.
- Parent widget.
- 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.
- Widget options.
- Default is system specific. (activeBackground/Background)
- Default is system specific. (background/Background)
- Same as borderwidth.
- Same as background.
- Widget border width. The default is system specific, but is usually a few pixels. (borderWidth/BorderWidth)
- Button background color. Default is system specific. (Button.background/Background)
- Cursor to use when the mouse pointer is moved over the button part of this widget. No default value. (Button.cursor/Cursor)
- The border style to use for the up button. Default is RAISED. (Button.relief/Relief)
- The border style to use for the down button. Default is RAISED. (Button.relief/Relief)
- A function or method that should be called when a button is pressed. No default value. (command/Command)
- 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)
- The background color to use when the widget is disabled. The default is system specific. (disabledBackground/DisabledBackground)
- The text color to use when the widget is disabled. The default is system specific. (disabledForeground/DisabledForeground)
- Default is True. (exportSelection/ExportSelection)
- Same as foreground.
- The font to use in this widget. Default is system specific. (font/Font)
- Text color. Default is system specific. (foreground/Foreground)
- Format string. No default value. (format/Format)
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)
- Default is system specific. (highlightBackground/HighlightBackground)
- Default is system specific. (highlightColor/HighlightColor)
- No default value. (highlightThickness/HighlightThickness)
- Default is 1.0. (increment/Increment)
- Color used for the insertion cursor. (insertBackground/Foreground)
- 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)
- Together with insertontime, this option controls cursor blinking. Both values are given in milliseconds. (insertOffTime/OffTime)
- See insertofftime. (insertOnTime/OnTime)
- Width of the insertion cursor. Usually one or two pixels. (insertWidth/InsertWidth)
- No default value. (invalidCommand/InvalidCommand)
- Same as invalidcommand.
- Default is LEFT. (justify/Justify)
- Default is system specific. (readonlyBackground/ReadonlyBackground)
- Default is SUNKEN. (relief/Relief)
- Together with repeatinterval, this option controls button auto-repeat. Both values are given in milliseconds. (repeatDelay/RepeatDelay)
- See repeatdelay. (repeatInterval/RepeatInterval)
- Default is system specific. (selectBackground/Foreground)
- No default value. (selectBorderWidth/BorderWidth)
- Default is system specific. (selectForeground/Background)
- One of NORMAL, DISABLED, or “readonly”. Default is NORMAL. (state/State)
- 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)
- No default value. (textVariable/Variable)
- See from. (to/To)
- Validation mode. Default is NONE. (validate/Validate)
- Validation callback. No default value. (validateCommand/ValidateCommand)
- A tuple containing valid values for this widget. Overrides from/to/increment. (values/Values)
- Same as validatecommand.
- Widget width, in character units. Default is 20. (width/Width)
- If true, the up and down buttons will wrap around. (wrap/Wrap)
- 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.
- The widget contents, as a string.
- icursor(index) [#]
Moves the insertion cursor to the given index. This also sets the INSERT index.
- Where to move the cursor.
- identify(x, y) [#]
Identifies the widget element at the given location.
- One of “none”, “buttondown”, “buttonup”, or “entry”.
- index(index) [#]
Gets the numerical position corresponding to the given index.
- An index.
- 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.
- Where to insert the text.
- The text to insert.
- invoke(element) [#]
Invokes a spinbox button.
- 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.
- 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.
- Current horizontal mouse position.
- selection_adjust(index) [#]
Adjusts the selection to include also the given character. If index is already selected, do nothing.
- 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.