Python 3 – Tkinter Spinbox
Python 3 – Tkinter Spinbox
The Spinbox widget is a variation of the standard Tkinter Entry widget that allows you to select from a fixed number of values.
Syntax
This is the simple syntax for creating this widget—
w = Spinbox( master, option, ... )
Parameters
- master – This represents the parent window.
-
options – This is a list of the most commonly used options for this widget. These options can be used as comma-separated key-value pairs.
Number | Option and Description |
---|---|
1 | activebackground The color of the slider and arrow when the mouse is over them. |
2 | bg The color of the scroll bar and arrow when the mouse is not over them. |
3 | bd The width of the 3D border around the entire gutter, as well as the width of the 3D effect for the arrows and slider. The default value is no border around the gutter, and a 2-pixel border width for the arrows and slider. |
4 | command The procedure to be called whenever the scroll bar is moved. |
5 | cursor The cursor that appears when the mouse is over the scroll bar. |
6 | disabledbackground The background color used when the component is disabled. |
7 | disabledforeground The text color used when the component is disabled. |
8 | fg The text color. |
9 | font The font used in this component. |
10 | format The format string. No default value. |
11 | from_ Minimum value. Used with to to limit the range of the spin box. |
12 | justify Defaults to LEFT. |
13 | relief Defaults to SUNKEN. |
14 | repeatdelay Together with repeatinterval, this option controls the automatic repeating of the button. Both values are given in milliseconds. |
15 | repeatinterval See repeatdelay. |
16 | state One of NORMAL, DISABLED, or READONLY. The default value is NORMAL. |
17 | textvariable No default value. |
18 | to See from. |
19 | validate Validation mode. The default value is NONE. |
20 | validatecommand Validation callback. No default. |
21 | values A tuple containing valid values for this widget. Overrides from/to/increment. |
22 | vcmd Same as validatecommand. |
23 | width The width of the widget in characters. Defaults to 20. |
24 | wrap If true, the up and down buttons will wrap. |
25 | xscrollcommand Used to connect a spinbox field to a horizontal scrollbar. This option should be set to the set method of the corresponding scrollbar. |
Methods
The Spinbox object has the following methods −
Number | Method and Description |
---|---|
1 | delete(startindex [,endindex]) This method deletes a specific character or a range of text. |
2 | get(startindex [,endindex]) This method returns a specific character or a text segment. |
3 | identify(x, y) This method identifies the widget element at a specified position. |
4 | index(index) This method returns an absolute index based on a given index. |
5 | insert(index [,string]…) This method inserts a string at a specified index. |
6 | invoke(element) This method activates the spin button. |
Example
Please execute the following example:
from Tkinter import *
master = Tk()
w = Spinbox(master, from_ = 0, to = 10)
w.pack()
mainloop()
Result
When the above code is executed, the following output is produced: