Python Tkinter SpinBox

In this Python tutorial we will discuss the Tkinter SpinBox widget. This widget is used to take input from the user by offering them a range of values to pick from, which they navigate using arrow keys. In many ways, the SpinBox widget resembles the ComboBox widget, but with arrow keys to help the user.


Spinbox Example – Picking a Number

In this example we will demonstrate how to create a simple Tkinter SpinBox widget, and use it to pick from a range of values.

We also have the increment option, which changes by how much the value is incremented/decrements when the arrowheads are used. In the example below we have this set to 1, but you can change this if you wish.

We can also use link a variable to the Spinbox, which we can use set and get values to and from the Spinbox. For example, using the set() function on the variable to manually define a value for the Spinbox. Calling set(5) in the start will cause the Spinbox’s default value to be 5, instead of 0.

(Remember to actually assign to variable to the Spinbox in the textvariable option)

import tkinter as tk


class Window:
    def __init__(self, master):
        frame = tk.Frame(master, width = 200, height = 100)

        Var1 = tk.IntVar()
        Var1.set(5)
        
        spinbox = tk.Spinbox(master, from_ = 0, to = 10,
                                     increment = 1, textvariable = Var1)
        spinbox.pack(padx = 5, pady = 20)

        frame.pack(padx = 10, pady = 10, expand = True, fill = tk.BOTH)
        

root = tk.Tk()
window = Window(root)
root.mainloop()

The output:

Tkinter Spinbox Example

Spinbox Example – Picking a Value

In this example we will explore how to define a set of values ourselves, which we pass into the Spinbox when creating it. This allows us to make the SpinBox display more than just numbers, as from_ and to take integer values only.

We will create a list called values, where we have a bunch strings saved. We will then pass this into the values option of the Spinbox, and use the arrow heads to cycle between them.

Another interesting thing we can do is link the SpinBox to a function, so every time one of the arrowheads are pressed, the function will be called. We will link the SpinBox to a function that uses the get() function on the variable associated with the SpinBox to return it’s current value and display it in a label widget.

import tkinter as tk


class Window:
    def __init__(self, master):
        frame = tk.Frame(master, width = 200, height = 80)

        values = ["Apple", "Banana", "Cabbage", "Beans"]

        self.spinbox = tk.Spinbox(master, from_ = 0, to = 10,
                                  increment = 1, values = values,
                                  command = self.display)
        self.spinbox.pack(padx = 5, pady = 20)

        text = "Current Value: " + str(self.spinbox.get())
        self.label = tk.Label(master, text = text)
        self.label.pack(padx = 10, pady = 20)

        frame.pack(padx = 10, pady = 10, expand = True, fill = tk.BOTH)

        
    def display(self):
        text = "Current Value: " + str(self.spinbox.get())
        self.label.configure(text = text)
        

root = tk.Tk()
window = Window(root)
root.mainloop()

The output: