Python 3 – Tkinter Spinbox
A Spinbox in Tkinter is a widget that allows the user to select a value from a range of values by incrementing or decrementing the value using up/down arrows. In this article, we will discuss how to create and use a Spinbox widget in Python 3 using the Tkinter module.
Creating a Spinbox widget in Tkinter
We can create a Spinbox widget in Tkinter by using the Spinbox() method. The syntax for creating a Spinbox widget is as follows:
from tkinter import *
root = Tk()
spinbox = Spinbox(root, from_=0, to=10, increment=1)
spinbox.pack()
root.mainloop()
In the above code, we have created a Spinbox widget and set its minimum value to 0 and maximum value to 10 by using the from_
and to
parameters respectively. The increment
parameter represents the step size by which the value will increase/decrease when the user presses the up/down arrows. Finally, we have packed the Spinbox widget using the pack()
method and started the Tkinter mainloop.
Configuring the Spinbox widget
We can configure the Spinbox widget by using various parameters such as the state
, width
, background
, foreground
, etc. Let’s see how we can set these properties:
spinbox.config(state='readonly', width=5, background='white', foreground='black')
In the above code, we have set the Spinbox widget’s state to readonly
, i.e., the user cannot write a value in the Spinbox, but he can only select the value by using the up/down arrows. We have set the widget width to 5 characters, and set the background and foreground colors to white and black, respectively.
Retrieving the value of the Spinbox widget
We can retrieve the current value of the Spinbox widget by using the get()
method. The get()
method returns the selected value as a string, which we can convert into an integer or float, depending on the use case. Let’s see an example:
from tkinter import *
root = Tk()
spinbox = Spinbox(root, from_=0, to=10, increment=1)
spinbox.pack()
def getValue():
selected_value = int(spinbox.get())
print(selected_value)
button = Button(root, text='Get Selected Value', command=getValue)
button.pack()
root.mainloop()
In the above code, we have created a button widget that, when clicked, calls the getValue()
function to retrieve the current value of the Spinbox widget using the get()
method. We have converted the selected value into an integer and printed it to the console.
Using the Spinbox widget with a Label widget
We can use the Spinbox widget in our Tkinter GUI application along with other widgets such as the Label widget to display the selected value. Let’s see how we can create a Label widget that displays the selected value when the user changes the Spinbox value:
from tkinter import *
root = Tk()
spinbox = Spinbox(root, from_=0, to=10, increment=1)
spinbox.pack()
label = Label(root, text='Selected Value: ')
label.pack()
def getValue():
selected_value = int(spinbox.get())
label.config(text='Selected Value: {}'.format(selected_value))
button = Button(root, text='Get Selected Value', command=getValue)
button.pack()
root.mainloop()
In the above code, we have created a Label widget that displays the text ‘Selected Value: ‘. When the user changes the Spinbox value, the getValue()
function is called, which retrieves the selected value, updates the text of the Label widget to display the selected value along with the text ‘Selected Value: ‘.
Conclusion
In conclusion, the Spinbox widget in Tkinter is a useful widget that allows the user to select a value from a range of values using increment/decrement buttons. We can create and configure the Spinbox widget and retrieve its value using the get()
method. We can also use the Spinbox widget along with other widgets such as the Label widget to display the selected value. I hope this article was helpful in learning how to use the Spinbox widget in Tkinter.