Tk ttk widgets and themes
last modified November 24, 2026 Tcl 8.6
The classic Tk widgets draw their own borders and colours, which is why old Tk
applications look much the same on every system. The ttk package
replaces them with themed widgets whose appearance comes from a theme object.
Switching the theme restyles every widget at once, without touching the layout
code.
This article introduces the most useful ttk widgets, explains how styles are named and configured, and shows how to list, inspect and switch themes while the program runs. The scripts print the state of the widgets and exit on their own.
Why ttk
package require Tk already loads ttk in Tk 8.6, so the themed
widgets are available under names such as ttk::button and
ttk::treeview. They keep the familiar -text and
-command options but add a state subcommand, style
names and native-looking drawing.
package require Tk puts "current theme: [ttk::style theme use]" puts "available: [ttk::style theme names]" button .old -text "Classic" ttk::button .new -text "Themed" pack .old -padx 10 -pady 4 pack .new -padx 10 -pady 4 update puts "classic class: [winfo class .old]" puts "themed class: [winfo class .new]" puts "button padding: [ttk::style lookup TButton -padding]" destroy .
The two buttons look different because they belong to different classes:
Button for the classic widget and TButton for the
themed one. The padding came from the style database, not from an option on the
widget. That indirection is the point of ttk: appearance lives in styles and
themes, behaviour lives in the widget.
$ wish why_ttk.tcl current theme: default available: clam alt default classic classic class: Button themed class: TButton button padding: 3 3
ttk::button and ttk::label
Themed buttons and labels take the same text options as their classic
counterparts. A label can still follow a variable through
-textvariable, and a button runs its command on
invoke or a mouse click. The difference appears when the button
must be switched off: ttk widgets use the state subcommand instead
of the -state option.
package require Tk
set clicks 0
ttk::label .caption -text "Clicks:"
ttk::label .value -textvariable clicks -width 4 -anchor center
ttk::button .add -text "Add" -command {incr clicks}
ttk::button .off -text "Disable" -command {.add state disabled}
pack .caption .value .add .off -padx 10 -pady 4
.add invoke
.add invoke
update
puts "clicks: $clicks"
puts "button state: [.add state]"
.off invoke
.add invoke
update
puts "after disable: [.add state]"
puts "clicks: $clicks"
.add state !disabled
.add invoke
puts "after enable: $clicks"
destroy .
state with no argument returns the current state flags, here an
empty string because the button is normal. Once disabled, the flag list contains
disabled and invoke does nothing, so the counter stays
at 2. The !disabled form removes one flag and leaves the rest
untouched, which is safer than resetting the whole state.
$ wish button_label.tcl clicks: 2 button state: after disable: disabled clicks: 2 after enable: 3
ttk::combobox
A combobox combines an entry with a drop-down list. The
-values option holds the choices, get returns the
current text and current selects a choice by its index.
-state readonly allows picking from the list but prevents typing
arbitrary text.
package require Tk
set city "Bratislava"
ttk::label .caption -text "City:"
ttk::combobox .city -values {Bratislava Kosice Zilina} \
-textvariable city -state readonly -width 16
pack .caption .city -padx 10 -pady 4
update
puts "initial: $city"
.city current 1
update
puts "after current 1: [.city get]"
puts "current index: [.city current]"
.city set "Zilina"
update
puts "after set: [.city get]"
puts "variable: $city"
bind .city <<ComboboxSelected>> {puts "selected: [.city get]"}
event generate .city <<ComboboxSelected>>
puts "values: [.city cget -values]"
destroy .
The combobox was bound to city, so current 1 changed
the variable as well as the visible text, and set did the same with
a value from outside the list. The <<ComboboxSelected>>
virtual event fires when the user picks an entry; generating it by hand runs the
same binding in a test. The list of values can be read back and replaced at run
time.
$ wish combobox.tcl initial: Bratislava after current 1: Kosice current index: 1 after set: Zilina variable: Zilina selected: Zilina values: Bratislava Kosice Zilina
ttk::notebook
A notebook stacks pages behind tabs. Each page is an ordinary frame created as a
child of the notebook and added with add; the -text
option becomes the tab label. Only the selected page is mapped, so a notebook is
a compact way to organise options.
package require Tk ttk::notebook .nb frame .nb.general frame .nb.network ttk::label .nb.general.user -text "User name and language" ttk::label .nb.network.port -text "Proxy host and port" pack .nb.general.user -padx 10 -pady 20 pack .nb.network.port -padx 10 -pady 20 .nb add .nb.general -text "General" .nb add .nb.network -text "Network" pack .nb -padx 10 -pady 10 .nb select .nb.network update puts "selected: [.nb select]" puts "index: [.nb index [.nb select]]" puts "tabs: [.nb tabs]" .nb tab .nb.general -text "Main" puts "tab text: [.nb tab .nb.general -text]" .nb select .nb.general puts "changed: [.nb select]" destroy .
select takes a page path and returns the selected one when called
without an argument; index converts it to a number and
tabs lists all pages in display order. tab configures
one tab, which is how a title changes after a file is renamed. Bindings on
<<NotebookTabChanged>> are the usual place to load a
page only when it becomes visible.
$ wish notebook.tcl selected: .nb.network index: 1 tabs: .nb.general .nb.network tab text: Main changed: .nb.general
ttk::panedwindow
A panedwindow divides its area with a draggable sash. Unlike a frame, it accepts
child panes that the user can resize, and -weight decides how extra
space is shared when the window grows. The sash position is measured in pixels
from the left or top edge.
package require Tk wm geometry . 420x200 ttk::panedwindow .pw -orient horizontal frame .pw.list -width 120 -height 80 frame .pw.detail -width 200 -height 80 ttk::label .pw.list.title -text "Items" ttk::label .pw.detail.title -text "Details" pack .pw.list.title -padx 8 -pady 8 pack .pw.detail.title -padx 8 -pady 8 .pw add .pw.list -weight 1 .pw add .pw.detail -weight 2 pack .pw -fill both -expand 1 -padx 8 -pady 8 update puts "panes: [.pw panes]" puts "sash: [.pw sashpos 0]" .pw sashpos 0 160 update puts "moved: [.pw sashpos 0]" puts "orient: [.pw cget -orient]" destroy .
The panes are the frames added with add, not their labels.
sashpos 0 reads the first sash position, and the two-argument form
moves it; the request was honoured at 160 pixels. The weights 1 and 2 made the
initial split favour the detail pane, which is typical for a list and a detail
view side by side.
$ wish panedwindow.tcl panes: .pw.list .pw.detail sash: 149 moved: 160 orient: horizontal
ttk::progressbar
A progressbar reports how much work is done. In determinate mode the application
sets -value or calls step, which adds the step amount
and wraps at -maximum. In indeterminate mode the bar animates while
a task of unknown length runs, controlled by start and
stop.
package require Tk
ttk::progressbar .bar -orient horizontal -length 220 \
-maximum 100 -mode determinate -value 0
pack .bar -padx 10 -pady 10
.bar step 25
puts "after step 25: [.bar cget -value]"
.bar step 25
puts "after step 50: [.bar cget -value]"
.bar configure -value 60
puts "value set: [.bar cget -value]"
puts "mode: [.bar cget -mode]"
ttk::progressbar .busy -orient horizontal -length 220 -mode indeterminate
pack .busy -padx 10 -pady 10
.busy start 20
puts "busy mode: [.busy cget -mode]"
after 300 {
.busy stop
puts "busy stopped"
destroy .
}
Two steps of 25 brought the value to 50.0, and the direct assignment set 60.
The indeterminate bar needs the event loop to animate, so the script stops it
with after and then destroys the window; without that timer the
program would keep running. The Tcl after command page
covers the timer form used here.
$ wish progressbar.tcl after step 25: 25.0 after step 50: 50.0 value set: 60 mode: determinate busy mode: indeterminate busy stopped
ttk::style and themes
Every ttk widget draws through a style. Built-in styles are named after their
class, such as TButton, and a new style is created by prefixing a
name: Danger.TButton inherits everything from
TButton except the options you configure. The style database also
holds maps for different widget states and a layout tree of elements.
package require Tk
puts "themes: [ttk::style theme names]"
puts "current: [ttk::style theme use]"
ttk::style configure Danger.TButton \
-background "#b00020" -foreground white -padding 10
puts "padding: [ttk::style lookup Danger.TButton -padding]"
puts "colour: [ttk::style lookup Danger.TButton -background]"
ttk::style map Danger.TButton \
-background {active "#7a0016" disabled "#cccccc"}
puts "map: [ttk::style map Danger.TButton -background]"
ttk::button .delete -text "Delete" -style Danger.TButton
pack .delete -padx 10 -pady 10
update
puts "style: [.delete cget -style]"
puts "layout: [ttk::style layout Danger.TButton]"
destroy .
configure stores the normal appearance, lookup reads
one option, and map gives alternative values for states such as
active and disabled. The layout output shows the
element tree that draws the button: a border, a focus ring, padding and the
label. A custom style can change that tree with
ttk::style layout, but most applications only set options and maps.
$ wish style_basics.tcl
themes: clam alt default classic
current: default
padding: 10
colour: #b00020
map: active "#7a0016" disabled "#cccccc"
style: Danger.TButton
layout: Button.border -sticky nswe -border 1 -children {Button.focus -sticky nswe -children {Button.padding -sticky nswe -children {Button.label -sticky nswe}}}
Switching themes at run time
ttk::style theme use changes the theme for the whole application
and redraws every ttk widget. The names come from
ttk::style theme names; on this system they are
clam, alt, default and
classic. A combobox of those names makes a small theme switcher.
package require Tk
set themes [ttk::style theme names]
ttk::combobox .themes -values $themes -state readonly
.themes set [ttk::style theme use]
pack .themes -padx 10 -pady 6
ttk::button .apply -text "Apply theme" -command switch_theme
pack .apply -padx 10 -pady 6
proc switch_theme {} {
set chosen [.themes get]
if {[lsearch -exact [ttk::style theme names] $chosen] < 0} {
puts "unknown theme: $chosen"
return
}
ttk::style theme use $chosen
puts "theme: [ttk::style theme use]"
puts "padding: [ttk::style lookup TButton -padding]"
}
bind .themes <<ComboboxSelected>> switch_theme
.themes set clam
event generate .themes <<ComboboxSelected>>
.themes set alt
.apply invoke
puts "final theme: [ttk::style theme use]"
destroy .
The procedure checks the name against the available themes with
lsearch -exact before using it, because an unknown theme raises an
error; the Tcl lsearch command page covers the
switches. The padding lookup shows how much the
themes differ: clam uses 5 pixels, alt uses 1 1. The
same code could save the chosen theme in a configuration file and apply it at
startup, a common finishing touch for a Tk application.
$ wish theme_switch.tcl theme: clam padding: 5 theme: alt padding: 1 1 final theme: alt
Source
The ttk manual pages document each themed widget, the style subcommands and the theme names. The Tk manual covers the classic widgets that ttk replaces, and the ttk::style page lists the element and layout options shown above.
- Tk 8.6 manual
- Tcl 8.6 manual
- ttk themed widgets
- ttk::style command
- ttk::notebook widget
- ttk::progressbar widget
This article introduced the themed widget set, covered the button, label, combobox, notebook, panedwindow and progressbar, and showed how styles and themes control their appearance. Together with Tk variables and traces and Tk listbox and treeview, these widgets are enough to build a complete desktop tool.
Author
List all Tcl Tutorials.