17. tk Spinbox
17.1. Usage
The
tkinter.Spinbox widget allows the user to select a value by either typing it directly or using the up and down arrow buttons.A spinbox can display a range of numbers or a sequence of strings.
To create a spinbox widget, the general syntax is
(assuming import via
import tkinter as tk):- spinbox_widget = tk.Spinbox(parent, option=value)
parentis the window or frame object.Options can be passed as parameters separated by commas.A spinbox can be associated with aStringVar,IntVarorDoubleVarcontrol variable.
17.2. Using a Spinbox
import tkinter as tk
root = tk.Tk()
root.title("Spinbox Example")
root.geometry("400x100")
value = tk.IntVar(value=5)
spinbox = tk.Spinbox(
root,
from_=0,
to=10,
increment=1,
textvariable=value,
width=4,
font=("Arial", 16),
bg="lightblue",
fg="blue",
activebackground="blue",
borderwidth=1,
relief="sunken",
)
spinbox.pack(padx=10, pady=10)
root.mainloop()
Tasks
Modify the code so that the spinbox:
Modify the code to produce the spinbox shown above.
import tkinter as tk
root = tk.Tk()
root.title("Spinbox Question")
root.geometry("400x100")
value = tk.StringVar()
spinbox = tk.Spinbox(
root,
values=("UG", "E", "D", "C", "B", "A"),
textvariable=value,
width=5,
font=("Arial", 16),
bg="lightgreen",
fg="green",
activebackground="green",
borderwidth=2,
relief="groove",
)
spinbox.pack(padx=10, pady=10)
value.set("C")
root.mainloop()
17.3. Methods
- spinbox_widget.delete(first, last=None)
- Deletes one or more characters from the entry field.
- spinbox_widget.get()
- Returns the current value as a string.
- spinbox_widget.insert(index, text)
- Inserts text into the entry field.
- spinbox_widget.icursor(index)
- Moves the insertion cursor to the specified position.
- spinbox_widget.invoke(element)
- Simulates clicking one of the arrow buttons.
elementmay be"buttonup"or"buttondown".
- spinbox_widget.selection('range', start, end)
- Selects a range of text.
17.4. Control variable methods
- variable.get()
- Returns the current value stored in the control variable.
- variable.set(value)
- Sets the current value of the control variable.The displayed value updates automatically.
17.5. Parameter syntax
- spinbox_widget = tk.Spinbox(parent, option=value)
parentis the window or frame object.Options can be passed as parameters separated by commas.Parameters:
- activebackground
- Syntax:
spinbox_widget = tk.Spinbox(parent, activebackground="color")Description: Sets the background colour of the arrow buttons while active.Default: SystemButtonFace
- background
- bg
- Syntax:
spinbox_widget = tk.Spinbox(parent, bg="color")Description: Sets the background colour.Default: SystemWindow
- borderwidth
- bd
- Syntax:
spinbox_widget = tk.Spinbox(parent, borderwidth=value)Description: Sets the border width.Default: 2
- buttonbackground
- Syntax:
spinbox_widget = tk.Spinbox(parent, buttonbackground="color")Description: Sets the background colour of the up and down arrow buttons.Default: SystemButtonFace
- command
- Syntax:
spinbox_widget = tk.Spinbox(parent, command=function)Description: Calls a function whenever the arrow buttons change the value.
- cursor
- Syntax:
spinbox_widget = tk.Spinbox(parent, cursor="cursor")Description: Sets the mouse cursor.
- disabledbackground
- Syntax:
spinbox_widget = tk.Spinbox(parent, disabledbackground="color")Description: Sets the background colour when the widget is disabled.Default: SystemButtonFace
- disabledforeground
- Syntax:
spinbox_widget = tk.Spinbox(parent, disabledforeground="color")Description: Sets the text colour when the widget is disabled.Default: SystemDisabledText
- exportselection
- Syntax:
spinbox_widget = tk.Spinbox(parent, exportselection=boolean)Description: Determines whether selected text is automatically copied to the clipboard.Default: 1
- font
- Syntax:
spinbox_widget = tk.Spinbox(parent, font=("font", size, style))Description: Sets the font used to display the value.
- foreground
- fg
- Syntax:
spinbox_widget = tk.Spinbox(parent, fg="color")Description: Sets the text colour.
- format
- Syntax:
spinbox_widget = tk.Spinbox(parent, format="%format")Description: Specifies how numeric values are displayed.
- from_
- Syntax:
spinbox_widget = tk.Spinbox(parent, from_=value)Description: Sets the minimum numeric value.Default: 0
- increment
- Syntax:
spinbox_widget = tk.Spinbox(parent, increment=value)Description: Sets the amount added or subtracted each time an arrow button is pressed.Default: 1
- justify
- Syntax:
spinbox_widget = tk.Spinbox(parent, justify="position")Description: Aligns the displayed text.Valid values:left,centerandright.
- readonlybackground
- Syntax:
spinbox_widget = tk.Spinbox(parent, readonlybackground="color")Description: Sets the background colour whenstate="readonly".Default: SystemButtonFace
- relief
- Syntax:
spinbox_widget = tk.Spinbox(parent, relief="style")Description: Sets the border style of the widget.Default:sunken
- repeatdelay
- Syntax:
spinbox_widget = tk.Spinbox(parent, repeatdelay=milliseconds)Description: Specifies how long to wait before the value begins auto-repeating while an arrow button is held down.Default: 400
- repeatinterval
- Syntax:
spinbox_widget = tk.Spinbox(parent, repeatinterval=milliseconds)Description: Specifies the time between repeated value changes while an arrow button is held down.Default: 100
- state
- Syntax:
spinbox_widget = tk.Spinbox(parent, state="state")Description: Sets the widget state.Valid values:normal,readonlyanddisabled.
- textvariable
- Syntax:
spinbox_widget = tk.Spinbox(parent, textvariable=variable)Description: Associates a control variable with the widget.
- to
- Syntax:
spinbox_widget = tk.Spinbox(parent, to=value)Description: Sets the maximum numeric value.Default: 0
- values
- Syntax:
spinbox_widget = tk.Spinbox(parent, values=("A", "B", "C"))Description: Displays a fixed sequence of values instead of a numeric range.
- width
- Syntax:
spinbox_widget = tk.Spinbox(parent, width=value)Description: Sets the width of the entry field.
- wrap
- Syntax:
spinbox_widget = tk.Spinbox(parent, wrap=True)Description: Wraps from the maximum value back to the minimum value.Default: False
17.6. Default options
Code to display the default value for each
Spinbox option is shown below.import tkinter as tk
root = tk.Tk()
widget = tk.Spinbox(root)
widget_options = widget.keys()
for option in widget_options:
print(f"{option}: {widget.cget(option)}") # cget retrieves the current value of the option