Tkinter Spinbox Tutorial
The Tkinter Spinbox control is also an input control. It is actually a combination of an Entry and a Button that allows the user to click the up/down button with the mouse or press the up/down arrow keys to increase and decrease the value in a certain value range. Alternatively, values can be entered directly here.
The constructor for creating Spinbox is as follows.
Spinbox(master, options, ...)
The first parameter of the Spinbox( ) method is the parent object, indicating which parent object this Spinbox will be built in. The following are the other commonly used options parameters within the Spinbox( ) method.
(1) activebackground
: the background color of the Spinbox control when the mouse cursor is on it.
(2) bg
: the background color.
(3)borderwidth or bd
: the width of the 3D border, the default is two pixels.
(4) command
: this function will be executed automatically when the user changes the options.
(5) cursor
: the shape of the cursor when the mouse cursor is on the Spinbox control.
(6) disablebackground
: the background color in the Disabled state.
(7) disableforeground
: the foreground color when the mouse cursor is in Disabled state.
(8) fg
: the foreground color of the text.
(9) font
: font shape.
(10) format
: the formatted string.
(11) from_
: initial value of the range value.
(12) increment
: the amount of value added or subtracted for each click of the up/down button.
(13) justify
: the last line of alignment when there are multiple lines of text, can take the value of LEFT/CENTER/RIGHT (left/center/right), the default is center alignment.
(14) relief
: the default is FLAT, you can change the appearance of the border.
(15) repeatdelay
: you can set the interval time to click up/down button to change the number, the unit is ms, the default is 300.
(16) state
: If set to DISABLE, the Spinbox is temporarily unavailable, the default is NORMAL, can also be set to READONLY.
(17) textvariable
: You can set it to display in a variable way.
(18) values
: can be a tuple or other sequence of values.
(19) to
: the end value of the range value.
(20) width
: for vertical Spinbox this is the width of the slot, for horizontal Spinbox this is the height of the slot.
(21) wrap
: click the up/down button to make the value restart.
(22) xscrollcommand
: use scrollbar in x-axis.
Tkinter Spinbox Tutorial Example#1
First experience with Spinbox control. The reader can use the mouse to click the up/down buttons to experience the increase or decrease in value, or press the up arrow/down arrow keys to experience it. The value range of this Spinbox is 10 to 30, and the amount of value added or subtracted is 2 each time.
from tkinter import *
root = Tk()
root.title("apidemos.com")
root.geometry("300x100")
spin = Spinbox(root,from_=10,to=30,increment=2)
spin.pack(pady=20)
root.mainloop()
Output:
Note: If you want to change the value with the up/down arrow keys, you must first place the insertion point in the value area.