SPINBOX IN TKINTER
In this tutorial, we are going to learn about the spinbox widget in tkinter that can be used as a replacement for the entry widget. We will learn about its features and methods along with a handy example.
What is Spinbox in Tkinter?
The Spinbox widget in tkinter is an alternative for the Entry widget. You can use the spinbox widget for selecting from a number of options. The syntax for using the spinbox widget is:
Spinbox_tk = Spinbox ( window, features )
Spinbox features and properties are:
1 | activebackground | Shows the spinbox background color upon the mouse hover |
2 | bg | It shows the background color of the widget. |
3 | bd | It shows the width of the border in pixels |
4 | command | It is used to set the function call whenever it is called. |
5 | cursor | You can use the cursor as different shapes on the screen as circle, dot etc. |
6 | disabledbackground | It shows the color of the background when the spinbox is disabled. |
7 | disabledforeground | It shows the color of the foreground when the spinbox is disabled. |
8 | fg | Determines the foreground color of font that is used for the spinbox. |
9 | font | It shows the font type of the widget. |
10 | format | Used for formatting the string in spinbox. |
11 | from_ | Specifies starting end of the spinbox range. |
12 | justify | It is used to display text on button in RIGHT, LEFT and CENTER justified positions. |
13 | relief | This displays different types of borders like SUNKEN, RAISED, GROOVE, and RIDGE. |
14 | repeatdelay | This option is used to define the duration when a button clicks through one range option to another. Time is measured in ms. |
15 | repeatinterval | It is quite near to repeatdelay function. |
16 | state | This represents DISABLED or NORMAL state of the spinbox. |
17 | textvariable | By using this feature, you can easily update the text of the widget whenever you want. |
18 | to | Specifies ending point of the spinbox range. |
19 | validate | Used for validating the widget’s value. |
20 | validatecommand | Used for calling back a function that is used for the validation of the widget information. |
21 | values | A tuple containing values for the spinbox widget. |
22 | vcmd | Equivalent to validation command. |
23 | width | Specifies the width of the widget. |
24 | wrap | This option is used for wrapping a button up and down the spinbox. |
25 | xscrollcommand | If you have a scrollable spinbox widget, then xscrollcommand will be used as a .set() method for the horizontal scrollbar. |
Methods for a spinbox widget are:
1 | delete(startindex, endindex) | This method is used to delete characters within the specified range. |
2 | get(startindex, endindex) | It fetches the characters available in the given range. |
3 | identify(x, y) | It identifies the widget’s elements in a specified range. |
4 | index(index) | It fetches the absolute index of the specified index. |
5 | insert(index, string) | It is used for inserting a string through an index value. |
6 | invoke(element) | It is used for supplicating the callback functions linked with the spinbox widget. |
Creating Spinbox
You can create simple a spinbox widget as follows:
from tkinter import * screen = Tk() screen.geometry('300x300') spinbox_tk = Spinbox(screen, from_=0, to=100) spinbox_tk.pack() mainloop()
Output will be:
References have been used from here