The Spinbox widget in Tkinter is a user interface element that allows users to select a value from a predefined range of values. Unlike the Entry widget, which allows for free-form text input, the Spinbox provides a fixed set of values that users can scroll through using up and down arrows.
w = Spinbox(parent, options)
• parent: The parent widget or window where the Spinbox will be placed.
• options: Various configuration options for the Spinbox (listed below).
| SN | Option | Description |
|---|---|---|
| 1 | activebackground | Background color of the widget when it has focus. |
| 2 | bg | Background color of the widget. |
| 3 | bd | Border width of the widget. |
| 4 | command | Callback function called each time the value changes. |
| 5 | cursor | Mouse pointer type when it hovers over the widget (e.g., arrow, dot). |
| 6 | disabledbackground | Background color when the widget is disabled. |
| 7 | disabledforeground | Foreground color when the widget is disabled. |
| 8 | fg | Foreground color of the widget. |
| 9 | font | Font type of the widget content. |
| 10 | format | Format string for the widget value. |
| 11 | from_ | Starting value of the range of values. |
| 12 | justify | Justification of the text (default is LEFT). |
| 13 | relief | Type of border (default is SUNKEN). |
| 14 | repeatdelay | Delay before the value starts auto-repeating when the button is held down (in milliseconds). |
| 15 | repeatinterval | Interval between repeated values (in milliseconds). |
| 16 | state | State of the widget. Possible values are NORMAL, DISABLED, or READONLY. |
| 17 | textvariable | Control variable that manages the widget's text. |
| 18 | to | Maximum value of the range. |
| 19 | validate | Validation method for widget value. |
| 20 | validatecommand | Callback function for validating the widget content. |
| 21 | values | Tuple of predefined values for the Spinbox. |
| 22 | vcmd | Validation command (same as validatecommand). |
| 23 | width | Width of the widget in characters. |
| 24 | wrap | Wraps up the Spinbox values if set to TRUE. |
| 25 | xscrollcommand | Set to the set() method of Scrollbar to make the Spinbox horizontally scrollable. |
| SN | Method | Description |
|---|---|---|
| 1 | delete(startindex, endindex) | Deletes characters in the specified range. |
| 2 | get(startindex, endindex) | Gets characters from the specified range. |
| 3 | identify(x, y) | Identifies the widget's element within the specified coordinates. |
| 4 | index(index) | Gets the absolute index of the specified index. |
| 5 | insert(index, string) | Inserts the specified string at the given index. |
| 6 | invoke(element) | Invokes the callback associated with the widget. |
Here's a basic example demonstrating how to create and use a Spinbox in a Tkinter application:
from tkinter import *
def on_value_change():
value = spinbox.get()
print(f"Selected value: {value}")
# Create the main window
root = Tk()
root.title("Spinbox Example")
# Create a Spinbox widget
spinbox = Spinbox(root, from_=0, to=10, increment=1, command=on_value_change, width=10)
spinbox.pack(pady=20)
# Run the Tkinter event loop
root.mainloop()
• from_: Sets the starting value of the range.
• to: Sets the ending value of the range.
• increment: Sets the step size for each increment.
• command: Specifies a callback function to be called whenever the value changes.
• width: Specifies the width of the Spinbox in characters.
This example creates a Spinbox that allows users to select a value between 0 and 10. The on_value_change function is called whenever the value in the Spinbox is changed, and it prints the selected value to the console.