Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 40 ยท Widgets Basics

Tkinter Scale, Spinbox and Progress Widgets

Pick numbers with Tkinter's Scale slider and Spinbox, set range, resolution and increment, react to changes, and show progress with the ttk Progressbar.

Not every number should be typed. A Scale is a slider for picking a value in a range (volume, brightness, zoom). A Spinbox is an entry with up and down arrows (quantity, hour, month). A Progressbar shows how far a task has got. All three work with numbers and with control variables, and all three have a few options that behave in surprising ways, which this lesson makes clear.

Scale

Python
import tkinter as tk

root = tk.Tk()
volume = tk.IntVar(value=40)

scale = tk.Scale(root, from_=0, to=100, orient="horizontal", length=240,
                 variable=volume, label="Volume", command=lambda v: print("value", v))
scale.pack(padx=20, pady=20)
root.mainloop()

from_ has a trailing underscore because from is a Python keyword. The important options:

Option Meaning
from_, to Range ends (to may be smaller for a reversed slider)
orient "horizontal" or "vertical"
resolution Step size: 1 (default), 0.1, 5...
tickinterval Draw tick labels every this many units
length Length of the track in pixels
showvalue Show the number above the slider (default on)
variable Linked IntVar or DoubleVar
command Function called on every change with the value as a string
digits Digits used for conversion; rarely needed
sliderlength, troughcolor, label, state Looks and behaviour

Three behaviours to remember:

  1. The command callback receives the value as a string ("40", "0.5"), not a number. Convert it, or read the linked variable instead.
  2. resolution=1 on a float range rounds to integers. For from_=0, to=1 you need resolution=0.01, otherwise the slider only reaches 0 and 1.
  3. command is also invoked when you call scale.set(...), but not when you change the variable.
Python
opacity = tk.DoubleVar(value=1.0)
tk.Scale(root, from_=0.1, to=1.0, resolution=0.05, orient="horizontal",
         variable=opacity,
         command=lambda v: root.attributes("-alpha", float(v))).pack()

What the slider rounds to

The rule is simple: values snap to from_ + n * resolution, and results are clamped to the range. Re-implementing it makes the string-versus-number behaviour and the rounding visible.

Python Stand-in: Scale snapping and the string callback

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Spinbox

A Spinbox combines an entry and two arrow buttons. You give it either a numeric range or a fixed list of values.

Python
import tkinter as tk

root = tk.Tk()
quantity = tk.IntVar(value=1)

tk.Spinbox(root, from_=1, to=10, textvariable=quantity, width=5).pack(pady=6)
tk.Spinbox(root, values=("Mon", "Tue", "Wed", "Thu", "Fri"), width=6, wrap=True).pack(pady=6)
tk.Spinbox(root, from_=0, to=1, increment=0.05, format="%.2f", width=6).pack(pady=6)
root.mainloop()
Option Meaning
from_, to, increment Numeric range and step size
values A tuple of strings to cycle through (overrides the range)
wrap After the last value, go back to the first
format printf-style, for example "%02.0f" shows 07
command Called when an arrow is pressed
textvariable Linked StringVar (even for numbers)
state "readonly" to forbid typing, so only the arrows change it
validate, validatecommand Same validation hooks as Entry

Because a Spinbox is an entry, a user can type anything: from_ and to limit only the arrows. When typing is allowed you still need to validate, using the same register() technique as in The Entry Widget, or set state="readonly". Read the value with get() and convert it yourself, since it always returns a string.

Python
def read_quantity():
    try:
        value = int(spin.get())
    except ValueError:
        return None
    return value if 1 <= value <= 10 else None

Stepping logic

The arrows move by increment, clamp at the ends unless wrap=True, and stay formatted. Here is that logic as a testable class, using decimals to avoid floating-point noise from repeated addition:

Python Stand-in: Spinbox arrows, clamp and wrap

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Scale versus Spinbox

Question Scale Spinbox
Precise typed value needed? No Yes
Value is approximate (volume, brightness)? Yes Possible
Few discrete options (months)? Possible Better with values=
Wide range, quick adjustments? Better Slow

Progressbar

ttk.Progressbar (a ttk widget, so import ttk) shows progress. Two modes:

  • determinate: you know the amount; set value from 0 to maximum (default 100).
  • indeterminate: you do not know how long it takes; the bar bounces back and forth.
Python
import tkinter as tk
from tkinter import ttk

root = tk.Tk()
bar = ttk.Progressbar(root, orient="horizontal", length=240, mode="determinate", maximum=100)
bar.pack(padx=20, pady=20)

def step():
    if bar["value"] < 100:
        bar["value"] += 10          # or bar.step(10)
        root.after(200, step)       # never use time.sleep here

step()

busy = ttk.Progressbar(root, mode="indeterminate", length=240)
busy.pack(pady=(0, 20))
busy.start(15)                      # move every 15 ms; busy.stop() ends it
root.mainloop()

Updating a bar from a loop works only if the loop returns control to Tk: either drive it with after() as above, or call root.update_idletasks() after each change. See The Event Loop for why. Bind variable= to a DoubleVar to update the bar by setting that variable.

A progress bar's job is to show a fraction; the percentage text is yours to compute:

Python Stand-in: progress as a fraction

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What type is the argument passed to a Scale's command callback?

    Tk passes the value formatted as a string. Convert it, or read the linked IntVar/DoubleVar instead.
  2. 2. You want a slider from 0 to 1 in steps of 0.01. Which option is required?

    Scale snaps to multiples of resolution, which defaults to 1, so a 0..1 range would only give 0 and 1.
  3. 3. How do you stop users from typing arbitrary text into a Spinbox?

    from_ and to limit the arrows only. readonly allows changes only through the arrows.
  4. 4. Which Progressbar mode is right when you cannot know the total work?

    Indeterminate mode animates a bouncing bar; start() and stop() control it.

Summary

  • Scale is a slider: from_, to, resolution, orient, variable; its command receives a string.
  • Spinbox combines an entry with arrows: numeric range with increment or a values tuple; get() returns text.
  • Arrows clamp at the ends unless wrap=True; typing is unrestricted unless you validate or use readonly.
  • ttk.Progressbar has determinate and indeterminate modes; update it with after(), never with sleep.
  • Choose the widget by task: precise number, approximate value or discrete options.

Next: Frames and LabelFrames