Spin Box in Tkinter (GUI Programming) – Python Tkinter Tutorial
Spin Box: In Tkinter, the Spinbox widget in Python is used to select a value from the specified given range of values. This tutorial is all about Spinbox widget. We will discuss about and also see some examples to understand it better.
- Spinbox widget is an alternative to Entry widget, where user has to enter a numeric value as per users requirement but in specified range.
- This widget is used only useful when user need to chose from a given range of choices.
Tkinter Spinbox Widget
Syntax:
sbox = Spinbox(root, from_ , to_)
root: Here root parameter denotes the parent window.
from_: It indicates starting range.
to_: It indicates end range.
Let’s have some examples to understand it better
Example 1:
from tkinter import * #creating root window root = Tk() year= IntVar() #creating spinbox sbox = Spinbox(root, from_ = 1990, to = 2018, textvariable = year) sbox.pack(pady=20) root.geometry('350x250') root.title("PythonLobby") root.mainloop()
Output:
Note: Here we can observe that the values inside spinbox is editable. Which is not the good practice. “State” parameter is used to solve this issue. If will set state=”readonly” then the value inside spinbox cannot be editable. See below given example:-
Example 2:
from tkinter import * #creating root window root = Tk() year= IntVar() #creating spinbox sbox = Spinbox(root, from_ = 1990, to = 2018, textvariable = year, state="readonly") sbox.pack(pady=20) root.geometry('350x250') root.title("PythonLobby") root.mainloop()
Output:
Now the values inside spinbox cannot be editable.
Program Explanation in Detail
- First we have imported all modules of tkinter i.e.
from tkinter import *
- Initialized root as object variable for Tk().
- Root indicates the root window where our spinbox is placed.
- from_ and to_ is the range specified.
- Textvariable is the parameter is used to check the type of options. It may be Integer or String.
- For integer type we use IntVar() and for string we use StringVar() functions. Here we are using IntVar() because we are using string type options.
Parameters we can use with SpinBox widget
- bg: This is used for the background color of the widget
- bd: This is used for the border width of the widget
Parameter Name | Description |
---|---|
bg |
This is used for the background color of the widget. |
bd |
This is used for the border width of the widget |
command |
This is used to indicate the assigned function with the widget & it is called every time the when the state of the widget is changed. |
cursor |
This parameter, changes the mouse pointer type to the cursor type when hover over widget. |
activebackground |
This indicates the background color of the widget when it is under focus |
disabledbackground |
This is used to indicate the background color of the widget when disabled. |
disabledforeground |
This is used to indicate the foreground color of the widget when disabled. |
font |
This specifies the font type of text in the widget. |
fg |
This specifies the foreground color of the widget. |
format |
This is mainly used for the format string. There is no default value of this option. |
from_ |
This is used to indicate the starting range of the widget. |
justify |
This specifies the alignment of multiple lines in the label. The default value is LEFT. |
relief |
This indicates the type of border. The default value is SUNKEN. |
state |
This is used to represent the state of the widget. The default state is NORMAL. Other remaining values are DISABLED, read-only, etc. |
validate |
This is used to control how to validate the value of the widget |
to |
This represents the maximum range of the widget value. The other value is specified by the from_ option |
repeatdelay |
This is used to control the autorepeat button. The value should be in milliseconds. |
repeatinterval |
This is similar to repeatdelay option. |
validatecommand |
This associates with the function callback that is used for the validation of the content of the widget. |
xscrollcommand |
This option is mainly used with the set() method of the scrollbar widget to make this widget horizontally scroll-able |
wrap |
This is used to wrap-up the up and down button of the Spinbox |
width |
This indicates the width of the widget. |
vcmd |
This is similar to validatecommand. |
values |
This represents the tuple which contains the values for the widget |
textvariable |
This is used to control variable that is used to control the text of the widget |