Python Tkinter SpinBox (original) (raw)
Last Updated : 01 May, 2024
The Spinbox widget in Tkinter is a numerical input field that allows users to select a value from a predefined range by either typing directly into the widget or by using up and down arrow buttons to increment or decrement the value.
**Note: For more reference, you can read our article:
Tkinter Spinbox Widget Syntax
The syntax to use the Spinbox is given below:
**Syntax: Spinbox ( master, options)
**Parameters:
- **master: This parameter is used to represents the parent window.
- **options:There are many options which are available and they can be used as key-value pairs separated by commas.
Tkinter Spinbox Options
The following are commonly used Options that can be used with this widget:
- **activebackground: This option used to represent the background color when the slider and arrowheads is under the cursor.
- **bg: This option used to represent the normal background color displayed behind the label and indicator.
- **bd: This option used to represent the size of the border around the indicator and the default value is 2 pixels.
- **command: This option is associated with a function to be called when the state is changed.
- **cursor: By using this option, the mouse cursor will change to that pattern when it is over the type.
- **disabledforeground: This option used to represent the foreground color of the widget when it is disabled..
- **disabledbackground: This option used to represent the background color of the widget when it is disabled..
- **font: This option used to represent the font used for the text.
- **fg: This option used to represent the color used to render the text.
- **format: This option used to formatting the string and it’s has no default value.
- **from_: This option used to represent the minimum value.
- **justify: This option used to control how the text is justified: CENTER, LEFT, or RIGHT.
- **relief: This option used to represent the type of the border and It’s default value is set to SUNKEN.
- **repeatdelay: This option is used to control the button auto repeat and its default value is in milliseconds.
- **repeatinterval: This option is similar to repeatdelay.
- **state: This option used to represent the represents the state of the widget and its default value is NORMAL.
- **textvariable: This option used to control the behaviour of the widget text.
- **to: It specify the maximum limit of the widget value. The other is specified by the from_ option.
- **validate: This option is used to control how the widget value is validated.
- **validatecommand: This option is associated to the function callback which is used for the validation of the widget content.
- **values: This option used to represent the tuple containing the values for this widget.
- **vcmd: This option is same as validation command.
- **width: This option is used to represents the width of the widget.
- **wrap: This option wraps up the up and down button the Spinbox.
- **xscrollcommand: This options is set to the set() method of scrollbar to make this widget horizontally scrollable.
**Methods
Methods used in this widgets are as follows:
- **delete(startindex, endindex): This method is used to delete the characters present at the specified range.
- **get(startindex, endindex): This method is used to get the characters present in the specified range.
- **identify(x, y): This method is used to identify the widget’s element within the specified range.
- **index(index): This method is used to get the absolute value of the given index.
- **insert(index, string): This method is used to insert the string at the specified index.
- **invoke(element): This method is used to invoke the callback associated with the widget.
**SpinBox Widget in Tkinter Example
In this example, below code sets up a Tkinter window with a Spinbox widget allowing users to select values from 0 to 100. When the value changes, it prints the new value. The Spinbox has customizable appearance options like width, relief, and color, and it’s placed in the window with padding.
Python3 `
import tkinter as tk
def on_spinbox_change(): value = spinbox.get() print("Value changed to:", value)
root = tk.Tk() root.geometry("300x200")
Creating a Spinbox
spinbox = tk.Spinbox(root, from_=0, to=100, width=10, relief="sunken", repeatdelay=500, repeatinterval=100, font=("Arial", 12), bg="lightgrey", fg="blue", command=on_spinbox_change)
Setting options for the Spinbox
spinbox.config(state="normal", cursor="hand2", bd=3, justify="center", wrap=True)
Placing the Spinbox in the window
spinbox.pack(padx=20, pady=20)
root.mainloop()
`
**Output