Introduction to Tkinter Spinbox
Tkinter Spinbox widget is an alternative to the Entry widget. It can be used to replace ordinary entry where the user can choose from a limited number of values only. When there are a fixed number of values, and we want to select the values, we use a spinbox widget, a standard Tkinter entry widget variant. And it is very much like an ordinary widget except that it can specify the values to be allowed, which can be either range or a tuple. This spinbox widget is available only in the versions of python above 2.3.
Syntax of Tkinter Spinbox:
w = Spinbox(master, option,..)
where master represents the parent window, and option is the list of options that can be used for the widget, and options are the key-value pairs that are separated by commas.
Tkinter Spinbox Widget
The most commonly used list of options for the widget are:
- active background: This option represents the slider colour and the arrowheads colour when the mouse is placed over them.
- bg: This option represents the slider colour and the arrowheads colour when the mouse is not placed over them.
- bd: The trough is covered by three-d borders around the entire perimeter of the trough, and this option represents its width. It also represents the width of the three-d effect on arrowheads and slider. By default, the trough has no borders around the perimeter of the trough, and the arrowheads and slider have a two-pixel border.
- command: This option calls the procedure whenever there is a movement of the scrollbar.
- font: This represents the font to be used in the widget.
- fg: This represents the color of the text in the spinbox.
- cursor: When the mover is present over the scrollbar, this option appears.
- disabledbackground: Whenever the widget is disabled, this option represents the background color to be used.
- disabledforeground: Whenever the widget is disabled, this option represents the text color to be used.
- format: This option represents the string format, and there is no default value for this.
- from: This option represents the minimum value that can be used to limit the range in the spinbox.
- justify: The default value for this option is LEFT.
- repeatDelay: This option and repeat interval control the auto-repeat button, and both the values can be represented in milliseconds.
- repeatInterval: This option and repeat delay control the auto-repeat button, and both the values can be represented in milliseconds.
- state: This option can be either of NORMAL or DISABLED or read-only. The default value for this option is NORMAL.
- textvariable: There is no default value for this option text variable.
- to: This option represents the minimum value that can be used to limit the range in the spinbox.
- validate: This option represents the mode of validation. The default value for this option is NONE.
- validateCommand: This option represents the callback of validation. There is no default value for this option.
- values: This option represents the valid values contained in the tuple for the widget. This option can override from, to and increment.
- vcmd: This option represents the callback of validation. There is no default value for this option.
- wrap: There is a wrapping of up and down buttons if the condition is true.
- relief: SUNKEN is the default value for relief.
- width: This option represents the width of the characters in the widget. Twenty is the default value.
- xscrollcommand: The option allows the user to scroll the spinbox horizontally.
Methods of Tkinter Spinbox
There are several methods that can be implemented on spinbox objects, they are:
- Delete(startindex[,endindex]): When a specific character or a range of characters must be deleted, we used this method.
- get(startindex[,endindex]): When a specific character or a range of characters must be returned, we used this method.
- identify(x,y): Given the location, the widget element can be identified using this method.
- index(index): Depending on the index given, the index’s absolute value is returned using this method.
- insert(index,[,string]..): The specified location of the index is where the string is inserted by using this method.
- invoke(element): The button of the spinbox is invoked using this method.
- selection_clear(): This method helps in clearing the selection.
- selection_get(): The selected text is returned using this method. This method will raise an exception if there is no selection currently.
- selection(‘range’, start, end): The text between the indices representing the start and end values is selected using this method.
- selection(‘to’, index): The text between the given index and the selection anchor is selected by using this method.
- selection(‘from’, index): The given index specifies the position at which the selection anchor must be pointed at. The selection anchor is initially set to zero.
Example of Tkinter Spinbox
Below are the examples of Tkinter Spinbox:
Python program using Tkinter Spinbox to select from a list of numbers from one to sixty incremented by one.
Code:
#Import tkinter package
import tkinter as tk
#Define a window name
root1 = tk.Tk()
text_variable = tk.DoubleVar()
#declare the spinbox widget by assigning values to from_, to and increment
spin_box = tk.Spinbox(
root1,
from_=1.0,
to=50.0,
increment=1.0,
textvariable=text_variable
)
#To show the content in the window
spin_box.pack()
root1.mainloop()
Output:
In the above code, we first import the Tkinter packages and libraries and then define a window by giving it a name. In our example, we call it tk. In our example, the values that we are trying to keep fixed must be floating-point numbers or double; hence we declare a variable and assign it to double. We then define the spinbox widget with from_, to values. In our example, we are allowing the user to select from a range of values 1.0 to 50.0, which gets incremented by 1 every time the user clicks on the upward arrowhead and the same is shown in the output snapshot above. The pack function is used to display the content in the window.
Conclusion
This article has learned about the Tkinter spinbox, the syntax attributes, methods, and examples of Tkinter spinbox. It can be used to create our own GUI’s along with other widgets provided by Tkinter.
Recommended Articles
This is a guide to Tkinter Spinbox. Here we discuss a brief overview of Tkinter Spinbox Widget, Methods, and its Examples along with its Code Implementation. You can also go through our other suggested articles to learn more –