One of the widgets that Tkinter provides is the Spinbox
The Spinbox widget allows you to select a value from a fixed range of numbers or a list of values. We will learn how to create a Spinbox widget and how to get the value that the user has selected.
Creating a Spinbox widget
To create a Spinbox widget, you need to import the Tkinter module and create a root window:
import tkinter as tk root = tk.Tk()
Then, you can use the
tk.Spinbox constructor to create a Spinbox widget and add it to the root window. The constructor takes several options, but the most important ones are:
to: These specify the minimum and maximum values of the range that the user can select. Note that
from_has an underscore at the end to avoid conflict with the Python keyword
values: This specifies a tuple of discrete values that the user can select. If this option is given, the
tooptions are ignored.
textvariable: This specifies a
tk.StringVarobject that holds the current value of the Spinbox. You can use this option to link the Spinbox value to other widgets or variables.
command: This specifies a function that will be executed whenever the value of the Spinbox changes.
For example, the following code creates a Spinbox widget that allows the user to select a number from 1 to 10:
spin_value = tk.StringVar(value=1) # create a StringVar object to store the Spinbox value spin_box = tk.Spinbox(root, from_=1, to=10, textvariable=spin_value) # create a Spinbox widget spin_box.pack() # add the Spinbox widget to the root window
Alternatively, you can create a Spinbox widget that allows the user to select a color from a list of values:
colors = ("Red", "Green", "Blue", "Yellow", "Pink") # create a tuple of colors spin_value = tk.StringVar(value=colors) # create a StringVar object to store the Spinbox value spin_box = tk.Spinbox(root, values=colors, textvariable=spin_value) # create a Spinbox widget spin_box.pack() # add the Spinbox widget to the root window
Getting the value from a Spinbox widget
There are two ways to get the value that the user has selected from a Spinbox widget:
- You can use the
get()method of the
tk.StringVarobject that is linked to the
textvariableoption of the Spinbox. For example:
print(spin_value.get()) # print the current value of the spin_value StringVar object
- You can use the
get()method of the Spinbox widget itself. For example:
print(spin_box.get()) # print the current value of the spin_box Spinbox widget
Both methods will return a string representation of the selected value. If you want to convert it to another data type, such as an integer or a float, you can use the built-in functions
float(). For example:
num = int(spin_box.get()) # convert the current value of the spin_box Spinbox widget to an integer print(num + 1) # print one more than the selected number
Executing a function when the value changes
If you want to perform some action when the user changes the value of the Spinbox widget, you can use the
command option to specify a function that will be executed automatically. For example, suppose you want to change the background color of the root window according to the color selected by the user in the Spinbox widget. You can define a function like this:
def change_color(): color = spin_value.get() # get the current value of the spin_value StringVar object root.config(bg=color) # change the background color of the root window
Then, you can pass this function as an argument to the
command option when creating the Spinbox widget:
spin_box = tk.Spinbox(root, values=colors, textvariable=spin_value, command=change_color) # create a Spinbox widget with command option
Now, whenever you select a different color in the Spinbox widget, you will see that the background color of the root window changes accordingly.