Python Tkinter Spinbox

Advertisements


The Spinbox widget is a variant of the standard Tkinter Entry widget, which can be used to select from a fixed number of values.

Syntax:

Here is the simple syntax to create this widget:

w = Spinbox( master, option, ... )

Parameters:

  • master: This represents the parent window.

  • options: Here is the list of most commonly used options for this widget. These options can be used as key-value pairs separated by commas.

OptionDescription
activebackground The color of the slider and arrowheads when the mouse is over them.
bg The color of the slider and arrowheads when the mouse is not over them.
bd The width of the 3-d borders around the entire perimeter of the trough, and also the width of the 3-d effects on the arrowheads and slider. Default is no border around the trough, and a 2-pixel border around the arrowheads and slider.
command A procedure to be called whenever the scrollbar is moved.
cursor The cursor that appears when the mouse is over the scrollbar.
disabledbackground The background color to use when the widget is disabled.
disabledforeground The text color to use when the widget is disabled.
fg Text color.
font The font to use in this widget.
format Format string. No default value.
from_ The minimum value. Used together with to to limit the spinbox range.
justify Default is LEFT
relief Default is SUNKEN.
repeatdelay Together with repeatinterval, this option controls button auto-repeat. Both values are given in milliseconds.
repeatinterval See repeatdelay.
state One of NORMAL, DISABLED, or "readonly". Default is NORMAL.
textvariable No default value.
to See from.
validate Validation mode. Default is NONE.
validatecommand Validation callback. No default value.
values A tuple containing valid values for this widget. Overrides from/to/increment.
vcmd Same as validatecommand.
width Widget width, in character units. Default is 20.
wrap If true, the up and down buttons will wrap around.
xscrollcommand Used to connect a spinbox field to a horizontal scrollbar. This option should be set to the set method of the corresponding scrollbar.

Methods:

Spinbox objects have these methods:

Methods & Description
delete(startindex [,endindex])
This method deletes a specific character or a range of text.
get(startindex [,endindex])
This method returns a specific character or a range of text.
identify(x, y)
Identifies the widget element at the given location.
index(index)
Returns the absolute value of an index based on the given index.
insert(index [,string]...)
This method inserts strings at the specified index location.
invoke(element)
Invokes a spinbox button.

Example:

Try the following example yourself:

from Tkinter import *

master = Tk()

w = Spinbox(master, from_=0, to=10)
w.pack()

mainloop()

When the above code is executed, it produces the following result:

TK Spinbox

Advertisements
Advertisements