Python Tkinter Spinbox – How to use

In this Tkinter tutorial, we will learn about the Python Tkinter Spinbox widget. And we also cover the following list of topics.

  • Python Tkinter Spinbox
  • Python Tkinter Spinbox Validate
  • Python Tkinter Spinbbox Set Value
  • Python Tkinter Spinbox Command
  • Python Tkinter Spinbox Get Value
  • Python Tkinter Spinbox Bind
  • Python Tkinter Spinbox Event
  • Python Tkinter Spinbox Format
  • Python Tkinter Spinbox Increment
  • Python Tkinter Spinbox Textvariable

Python Tkinter Spinbox

Python Tkinter Spinbox widget is a type of Entry widget with some advanced features. In this section, we will explore all the potential of Python Tkinter Spinbox.

  • Python Tkinter Spinbox is a type of entry widget with up-down arrows.
  • Range of data can be put inside the spinbox and user can navigate using up & down arrow.
  • Also, user can type information like we do in the Entry widget.

Source code:

In this code, we have created a simple Python Tkinter Spinbox widget.

  • Python Tkinter Spinbox requires range of values so that it can display the information.
  • from_ is the starting of the range of number and to is the finish line of the range.
  • Alternative to from_ and to we can also pass List or Tuple of information for the value option.
  • example: value = [1, 2, 3, 4, 5, 6]
from tkinter import *

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x200')
ws.config(bg='#31403C')

Spinbox(
    ws,
    from_=1,
    to=10,
    font=('sans-serif', 14), 

).pack(pady=20)


ws.mainloop()

Output:

In this example, the Python Tkinter Spinbox widget is displayed. The data inside it ranges from 1 to 10. Users can click on the upward or down button to navigate the data.

python tkinter spinbox
Python Tkinter Spinbox

Read: Python Tkinter Validation examples

Python Tkinter Spinbox Validate

Python Tkinter Spinbox validate option allows to validate the user input and avoids the non-standard values.

  • By default, validate is set to None
  • validate and validatecommand are two things that work hand in hand.
  • validate provides the mode of validation whereas validatecommands triggers the function or even that will occur.
  • Validate mode supports following options:
focusValidate whenever the Entry widget gets or loses focus
focusinValidate whenever the widget gets focus.
focusoutValidate whenever the widget loses focus.
keyValidate whenever any keystroke changes the widget’s contents.
allValidate in all the above situations.
noneThis is the default option value it Turns off validation. Please note that this is the string ‘none’, not the special Python value None.
Validation Mode in Python Tkinter Spinbox

Read: Python Tkinter Quiz 

Python Tkinter Spinbox Set Valu

In this section, we will learn about Python Tkinter Spinbox Set Value. We will see how to set a value in Python Tkinter Spinbox.

  • Python Tkinter Spinbox accepts range of values either in the form of from_ , to or List and tuple.
  • Incase from_ is provided then it becomes the default value
  • In case of list and tuple value on 0th index becomes the first value in Python Tkinter Spinbox.
  • set() function in Python Tkinter Spinbox is used customize the default value.
  • Set() function is applied either on the textvariable or on the variable used to store the Python Tkinter Spinbox.
  • In our example, you will notice that Python Tkinter Spinbox will display default value.

Source Code:

In this example, we have used the set() function to set the default value in Python Tkinter Spinbox.

  • set() function is applied on the textvariable.
from tkinter import *

def display_selected():
    Label(
        text=f'You selected {var.get()}', 
        font=('sans-serif', 14),
        bg='#6393A6'
        ).pack()

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x200')
ws.config(bg='#6393A6')

var = IntVar()
var.set(7)  # default value is 5
Spinbox(
    ws,
    textvariable=var,
    from_=1,
    to=10,
    increment=2,
    command=display_selected,
    font=('sans-serif', 18), 

).pack(pady=20)


ws.mainloop()

Output:

In this example, we have demonstrated how to set default values in Python Tkinter. You can see that first, we have set the default value as 5 then we changed it to 7. Every time the application is started it has the assigned default value.

python tkinter spinbox set
Python Tkinter Spinbox Set Value

Read: Python tkinter label

Python Tkinter Spinbox Command

In this section, we will learn about the Python Tkinter Spinbox command.

  • While using Python Tkinter Spinbox it is expected that something should happen when user clicks on the upward or downward key on the Python Tkinter Spinbox.
  • Functions holds the information of an activity and these functions are binded using the option ‘command’ in Python Tkinter Spinbox command.
  • In our example, we wil display the selected value evertime user will click on the upward or downward key of Python Tkinter Spinbox.

Source Code:

In this example, we have created a simple application using the Python Tkinter Spinbox widget.

  • The application has list of fruits and each fruit name can be accessed using Python Tkinter Spinbox widget.
  • everytime user clicks on the upward or downward arrow, function is triggered. This function is assigned using option ‘command’.
  • In our case, we have created a function display_selected() and we have passed this name in command option.
from tkinter import *

def display_selected():
    msg.config(
        text=f'You selected {var.get()}', 
        font=('sans-serif', 14),
        bg='#D97904'
        )

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x200')
ws.config(bg='#D97904')

var = StringVar()
Spinbox(
    ws,
    textvariable=var,
    value=['Grapes', 'Banana', 'Mango', 'Blueberries'],
    increment=2,
    width=10,
    command=display_selected,
    font=('sans-serif', 18), 

).pack(pady=20)

msg = Label(
    ws,
    text='',
    bg='#D97904'
)
msg.pack(pady=10)

ws.mainloop()

Output:

In this output, a list of fruits has been passed as a value in the Python Tkinter Spinbox widget. Every time the user switches to new fruit a message is displayed with the selected fruit name.

python tkinter spinbox command
Python Tkinter Spinbox command

Read: Python Tkinter Entry 

Python Tkinter Spinbox Get Value

In this section, we will learn about Python Tkinter Spinbox Get Value.

  • Get() function is used to fetch the value from the widget that is provided by the user.
  • By default, value fetched is in a string type but it can be typecast to other data types.
  • Using Get() function, we can fetch the value selected by user in Python Tkinter Spinbox.
  • In our example, we will fetch the value selected by user and also display it using Python Tkinter Label widget.

Source Code:

from tkinter import *

def display_selected():
    Label(
        text=f'You selected {var.get()}', 
        font=('sans-serif', 14),
        bg='#6393A6'
        ).pack()

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x200')
ws.config(bg='#6393A6')

var = IntVar()
Spinbox(
    ws,
    textvariable=var,
    from_=1,
    to=10,
    increment=2,
    command=display_selected,
    font=('sans-serif', 18), 

).pack(pady=20)

ws.mainloop()

Output:

In this example, we have used the get() method to fetch the value from the Python Tkinter Spinbox widget and we are displaying the selected value using the Python Tkinter Label widget.

python tkinter spinbox get
Python Tkinter Spinbox Get Value

Read: Python Tkinter Button

Python Tkinter Spinbox Bind

Python Tkinter supports a feature ‘bind’ that allows binding of an event with the widget. In this section, we will learn about Python Tkinter Spinbox Bind.

  • Bind has three components: Spinbox.bind(event_name, handler, add=None)
    • event_name is the activity that will trigger the handler. event could be press, release, motion, etc of button or any other pheripheral.
    • handler is the function associated with the event. In simple words, handler has the information that what to do when event is invoked.
    • add is None by default. You can replace None with ‘+’ sign. This will replace the previous handler with the current one. ‘+’ sign is provided on the current handler.
  • In our next section, we have explained Bind with an example.


Python Tkinter Spinbox Event

Event is any activity that happened. It could be button click, release, motion, etc. In this section, we will learn about Python Tkinter Spinbox Event.

  • Every event is binded with some widget like Python Tkinter Spinbox in this case.
  • The event invokes the handler or function. In simple words, every time event occurs an activity is binded with it.

Source Code:

In this example, we have created GUI based application using Python Tkinter wherein account balance is displayed every time user clicks on the arrowhead.

  • sb.bind('<Button-1>', 14), here sb is the variable assinged to Python Tkinter Spinbox.
  • '<Button-1>' is the mouse left-click event. Everytime user left-clicks then this event is called.
  • disp is the function name.
from tkinter import *

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x200')
ws.config(bg='#45858C')

def disp(event):
    msg.config(text=f'Account Balance: {sb.get()}')

sb = Spinbox(
    ws,
    from_=1000,
    to=100000,
    increment=100,
    format='%10.2f',
    font=('sans-serif', 14)
)
sb.pack(pady=20)
sb.bind('<Button-1>', disp)

msg = Label(
    ws,
    text= '',
    bg='#45858C',
    font=('sans-serif', 14)
)
msg.pack(pady=10)

ws.mainloop()

Output:

In this example, an event is invoked every time user clicks on the arrowhead. The function displays the account balance. This increases and decreases every time a user clicks on the arrowhead. Please note that if the user press and holds the arrowhead then the value in the Python Tkinter Spinbox changes but the value in the Python Tkinter Label widget remains the same. This is because the event invokes only on click.

python tkinter spinbox event
Python Tkinter Spinbox Event

Read: Python Tkinter Radiobutton

Python Tkinter Spinbox Format

Python Tkinter spinbox provides a Format option using which any format can be assigned on the numeric values.

  • While dealing with monetry database you want to set monetry value in a 1000.00 format then you can do using format option in Python Tkinter Spinbox.
  • Here 1000 can be any value but you want to provide .00 after any value.
  • to do so simply use this code fomat='%10.2f', here 10 is the place value and 2 is the number of zeros after point.
  • Here is an example to demonstrate the given situation.
from tkinter import *

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x200')
ws.config(bg='#296B73')

Spinbox(
    ws,
    from_=1000,
    to=100000,
    increment=100,
    format='%10.2f',
    font=('sans-serif', 14)
).pack(pady=20)


ws.mainloop()

Output:

In this example, we have used the format option in the Python Tkinter Spinbox widget. You can see that 2 zeros are added after the point in each value.

python tkinter spinbox format
Python Tkinter Spinbox Format

Read: Python Tkinter Checkbutton

Python Tkinter Spinbox Increment

In this section, we will cover Python Tkinter Spinbox increment. We will learn how to change the increment value.

  • By default, Python Tkinter Spinbox increments by 1 everytime user clicks on the upward arrow.
  • To customize this feature in Python Tkinter Spinbox, increment option is used.
  • Python Tkinter Spinbox will jump to next value according to the whatever value assigned in increment option.
  • for example, if thet start value is 1 and increment value is 2 that means spinbox will jump to 3, 5, 7, 9…n on every click on upward button.
  • In our example, we have displayed the same.

Source Code:

from tkinter import *

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x200')
ws.config(bg='#64732F')

Spinbox(
    ws,
    from_=1,
    to=10,
    increment=2,
    font=('sans-serif', 18), 

).pack(pady=20)

ws.mainloop()

Output:

In the example, we have demonstrated the increment option in Python Tkinter Spinbox. we have assigned the value of increment to 2 which means every time user clicks on the upward or downward button the value jumps 2 numbers.

python tkinter spinbox increment
Python Tkinter Spinbox Increment

Read: Python Tkinter Menu bar

Python Tkinter Spinbox Textvariable

In this section, we will learn about Python Tkinter Spinbox Textvariable.

  • Textvariables are used where the data is not fixed.
  • Textvariables works with all the widgets and are available for all the data types.
  • Textvariable need to be assigned with appropriate variable. Here are the options:
    • StringVar() is for strings and character.
    • IntVar() is for Integers (non-decimal numbers)
    • BooleanVar() is for True or False
    • DoubleVar() is for decimal numbers
  • The widget can be controlled with it’s textvariable. In our example, we have
from tkinter import *

def display_selected():
    msg.config(
        text=f'You selected {var.get()}', 
        font=('sans-serif', 14),
        bg='#296B73'
        )

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x200')
ws.config(bg='#296B73')

var = StringVar()
cities = ['Los Angeles', 'Chicago', 'Houston', 'Nashville', 'El Paso', 'Virginia', 'Texas', 'New York']

Spinbox(
    ws,
    textvariable=var,
    value=cities,
    increment=2,
    width=10,
    command=display_selected,
    font=('sans-serif', 18), 

).pack(pady=20)

msg = Label(
    ws,
    text='',
    bg='#296B73'
)
msg.pack(pady=10)

ws.mainloop()

Output:

In this output, Python Tkinter Spinbox is using textvariable to control the changing Label. Since the text is String so we are using StringVar() in this case.

python tkinter spinbox textvariable
Python Tkinter Spinbox Textvariable

You may also like to read the following articles.

In this tutorial, we have learned about the Python Tkinter Spinbox widget. Also, we have covered these topics.

  • Python Tkinter Spinbox
  • Python Tkinter Spinbox Validate
  • Python Tkinter Spinbbox Set Value
  • Python Tkinter Spinbox Command
  • Python Tkinter Spinbox Get Value
  • Python Tkinter Spinbox Bind
  • Python Tkinter Spinbox Event
  • Python Tkinter Spinbox Format
  • Python Tkinter Spinbox Increment
  • Python Tkinter Spinbox Textvariable