What is Spinbox widget?

Spinbox widget is similar to standard Tkinter Entry widget, It is also used to take input from the user, but here input is predefined or between some range. Like – if you want to take one number from the user between 1 to 100 then Spinbox can be used.

Example

The following Python script creates a window containing one Spinbox widget that takes one number from the user between 1 to 10. The user can increase or decrease the visible value on the entry field by clicking on up and down button.

You need to write following code in any text editor and then save it with .py extension. To execute the application, Type py SpinboxExample.py command in terminal. 

from tkinter import *

# Top level window
window = Tk()

window.title("Studyfied.com")
window.geometry('350x200')

# Create a spinbox
spinbox = Spinbox(window, from_=1, to=10)
spinbox.pack()

window.mainloop()

Output

The above code produces the following output in windows operating system.

Explanation

Following line creates a Tkinter window

from tkinter import *

window = Tk()

window.title("Studyfied.com")
window.geometry('350x200')

See explanation of Tkinter window – Tkinter top level window


This snippet creates a Spinbox widget, the first parameter is the name of the parent widget or window. Here it is “window”. And the remaining two are min and max range number, the user can increase or decrease it by up / down arrow button, You can also pass a tuple with some values instead of range. You can also provide other properties to customize the widget.

# Create a spinbox
spinbox = Spinbox(window, from_=1, to=10)
spinbox.pack()

The second line spinbox.pack(), is geometry manager that packs widgets into rows or columns.


Some spinbox properties

Property NameDescription
background / bgSets the background color of the widget, Default Is platform specific
borderwidth / bdThe width of the entry widgets border. The default is system specific but usually 1 to 2 pixels.
cursorThe cursor to display when the mouse moves over the widget. By default, the standard cursor is used.
buttoncursorCursor to display when the mouse moves over the up / down arrow button.
commandCommand to the user for arrow buttons similar to Tkinter button.
incrementDefines how much value to increase or decrease when up / down pressed.
disabledforegroundThe foreground color to use when the widget is disabled or invalid. The default is system specific.
exportselectionIf true, the selected text is automatically exported to the clipboard. The default is true.
fontFont to use for the widget. The default is system specific.
foreground / fgThe foreground color for this widget. The default is system specific
justifyAlign the text inside the entry field. Use one of LEFT, CENTER, or RIGHT. The default is LEFT.
textvariableAssociates the Tkinter variable (usually StringVar) with the contents of the entry widget.
padxHorizontal padding to add around the text. The default is 1 pixel.
padyVertical padding to add around the text. The default is 1 pixel.