Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 40 ยท Styling with ttk

ttk Styles and Themes: Custom Colors, Fonts and State Maps

Restyle Tkinter ttk widgets with ttk.Style: switch themes, configure custom style names, use map() for hover and disabled states, and set app-wide fonts.

Classic Tkinter widgets are styled one at a time with options. ttk widgets are styled in a different way: each widget has a style name (for example TButton), and the style says how that kind of widget looks. Change the style, and every widget using it changes. This gives you consistent apps with very little code, but it also feels unusual at first.

This lesson explains themes, Style.configure, Style.map, custom style names like Accent.TButton, and fonts. The window code is read-only (no display here). Runnable blocks model how style names resolve, which is the part people find confusing.

Themes

A theme is a complete set of looks for all ttk widgets. Which themes exist depends on the platform:

Python
from tkinter import ttk

style = ttk.Style()
print(style.theme_names())     # e.g. ('winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative')
print(style.theme_use())       # the current theme
style.theme_use("clam")        # switch

On Windows you typically see vista, winnative, xpnative, clam, alt, default and classic; on macOS aqua, clam, alt, default, classic; on Linux clam, alt, default, classic. Native themes (vista, aqua) look most natural but honor few color changes: a vista button ignores background. If you want custom colors, switch to clam, which is drawn by Tk itself and respects almost every style option. Call theme_use once, early, before creating widgets.

configure: change how a style looks

Python
style = ttk.Style()
style.theme_use("clam")

style.configure("TButton", padding=(12, 6), font=("Segoe UI", 11))
style.configure("TLabel", font=("Segoe UI", 11))
style.configure("Title.TLabel", font=("Segoe UI", 20, "bold"), foreground="#1f4e79")

After this, every ttk.Button has more padding and a larger font, and any label created with style="Title.TLabel" becomes a big blue heading:

Python
ttk.Label(root, text="Dashboard", style="Title.TLabel").pack(pady=10)
ttk.Label(root, text="Plain text").pack()            # uses TLabel

configure(style_name, **options) sets the default look; the available options depend on the widget and theme. Use style.lookup("TButton", "background") to read a value, and style.configure("TButton") (with no options) to list what is currently set. To discover options for a widget, call style.layout("TButton") and style.element_options("Button.label").

Custom style names

A custom style is named Prefix.TWidget, where TWidget is the base style class. The Prefix. part creates a variant that inherits from the base:

Style name Inherits from
Accent.TButton TButton
Danger.Accent.TButton Accent.TButton, then TButton
Card.TFrame TFrame
Heading.Treeview.Heading custom heading look

Resolving a style goes right to left through the dotted name. If an option is not set on Danger.Accent.TButton, ttk asks Accent.TButton, then TButton, then the theme default. The stand-in below is not Tkinter; it models this lookup so you can see why a variant only needs the options that differ.

Python Stand-in: style name inheritance

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.

The danger button gets its red background from itself, its white text from Accent.TButton and its padding from TButton.

map: colors that depend on the widget state

configure sets one value. Buttons, however, look different when hovered (active), pressed (pressed) or disabled. style.map assigns values per state combination:

Python
style.configure("Accent.TButton", background="#0067c0", foreground="white", borderwidth=0)
style.map(
    "Accent.TButton",
    background=[("disabled", "#cccccc"), ("pressed", "#004a8c"), ("active", "#1a7fd1")],
    foreground=[("disabled", "#888888")],
)

The button is blue; when the pointer is over it, it turns a lighter blue; while pressed, darker; when disabled it turns gray with gray text. The lists are checked in order, first match wins, so put the most specific or most important states first. A state may be negated with !: ("!disabled", "white"). Forgetting map is why many first ttk buttons "do nothing" on hover in the clam theme: the default state map sets a color you did not override.

The stand-in below models first-match-wins state maps, and the order trap:

Python Stand-in: style.map with first-match-wins

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.

With the wrong order, "pressed" never shows because "active" matches first.

Styling common widgets

Each widget class has its own style name and its own options:

Widget Style name Typical options
Button TButton background, foreground, padding, relief, font
Label TLabel foreground, background, font
Entry TEntry fieldbackground, foreground, padding
Frame TFrame background
Checkbutton TCheckbutton background, indicatorcolor
Notebook tab TNotebook.Tab padding, font, background
Treeview Treeview, Treeview.Heading rowheight, fieldbackground, font
Progressbar Horizontal.TProgressbar troughcolor, background, thickness
Scale Horizontal.TScale troughcolor, sliderlength

Note that the Treeview and orientation-specific styles (Horizontal.TProgressbar, Vertical.TScrollbar) do not follow the T prefix rule. Look them up with widget.winfo_class() or widget.cget("style").

A whole-app palette

A dark or branded look needs three layers: the root window background (a plain Tk option), the style defaults for each ttk class, and tk-only widgets such as Text and Listbox, which ignore styles. One function keeps this together:

Python
PALETTE = {"bg": "#1e1f22", "fg": "#dfe1e5", "field": "#2b2d30", "accent": "#3574f0"}

def apply_theme(root, p=PALETTE):
    style = ttk.Style(root)
    style.theme_use("clam")
    root.configure(bg=p["bg"])
    style.configure(".", background=p["bg"], foreground=p["fg"], fieldbackground=p["field"])
    style.configure("TButton", background=p["field"], padding=8)
    style.map("TButton", background=[("active", p["accent"])])
    style.configure("TEntry", insertcolor=p["fg"])

The special style name "." is the root of the style tree: options set there apply to every ttk widget unless overridden. Setting background, foreground and fieldbackground on "." is the quickest way to recolor an entire application. Classic widgets such as tk.Text need their own bg=, fg= and insertbackground= options.

Fonts

For consistent typography, change Tk's named fonts instead of setting a font on every widget:

Python
import tkinter.font as tkfont

for name in ("TkDefaultFont", "TkTextFont", "TkMenuFont", "TkHeadingFont"):
    tkfont.nametofont(name).configure(family="Segoe UI", size=11)

Every widget that uses a default font (including menus and most ttk widgets) immediately uses the new family and size. This is also the simplest way to implement a "larger text" setting. For a one-off font, create a Font object and keep it alive: big = tkfont.Font(family="Segoe UI", size=24, weight="bold").

Common mistakes

  • Using a native theme and expecting background to change. Switch to clam.
  • Calling style.configure("Button", ...). The class name is TButton.
  • Putting a state with a broader match before a narrower one in map.
  • Creating ttk.Style() before the root window exists, which makes Tkinter create a hidden default root.
  • Changing a style on a widget that uses a different style name (for example a Heading.TLabel ignores later changes to TLabel options it overrides).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the style name Accent.TButton mean?

    The prefix creates a variant; options it does not set come from TButton.
  2. 2. Which theme is the best choice when you want to set your own colors?

    clam is drawn by Tk itself and honors nearly every color option, while native themes ignore many.
  3. 3. In style.map, which entry wins when several state tuples match?

    Entries are checked in order, so put important states like disabled first.
  4. 4. What does style.configure('.', background='#222') do?

    '.' is the root of the style hierarchy.

Summary

  • Pick a theme with theme_use; use clam for custom colors.
  • configure sets defaults, map sets values per widget state (first match wins).
  • Create variants with Name.TWidget; they inherit from the base style.
  • The style "." styles everything; classic widgets need their own options.
  • Change named fonts for consistent typography.

Next: Treeview: Tables and Trees