ttk::intro(3tk)
π ttk::intro β bestie's guide to the Tk theme engine π
okay so this is ttk::intro, from the 3tk manual section, and it's literally just the β¨ introduction to the Tk theme engine β¨. no cap. let's get into it π«Ά
πΈ OVERVIEW πΈ
So the Tk themed widget set is built on a revised and enhanced version of TIP #48 (https://tip.tcl-lang.org/48), which is the specified style engine. iykyk π
The whole main character idea here is separating the widget's behavior from the widget's appearance β as much as physically possible, bestie. So like:
- Widget class bindings are the ones mainly responsible for keeping the widget state and invoking callbacks π₯
- Everything about how the widget looks is controlled by the style of the widget (aka the style of the elements of the widget) β¨
two jobs, two departments. periodt.
π THEMES π
A theme is just a collection of elements and styles that decide the look and feel of the whole widget set. and themes can be used to:
- isolate platform differences (X11 vs. classic Windows vs. XP vs. Aqua ...) π
- adapt to display limitations (low-color, grayscale, monochrome, tiny screens) π§
- accessibility (high contrast, large type) π
- application suite branding π
- blend in with the rest of the desktop (Gnome, KDE, Java) π¦
- and, of course: eye candy. π β¨
π ELEMENTS π
An element displays one individual part of a widget. like, a vertical scrollbar widget contains uparrow, downarrow, trough and slider elements. that's four lil guys doing their thing π₯Ί
Element names use a recursive dotted notation. so for example uparrow identifies a generic arrow element, while Scrollbar.uparrow and Combobox.uparrow identify widget-specific elements.
When the style engine goes looking for an element, it checks the specific name first β and if there's no element by that name, it looks for generic elements by stripping off successive leading components of the element name. detective behavior fr π
Like widgets, elements have options that say what to display and how to display it. For example, the text element (which displays a text string) has -text, -font, -foreground, -background, -underline, and -width options.
And the value of an element option is taken from β in precedence order:
- an option of the same name and type in the widget containing the element;
- a dynamic setting specified by
style mapand the current state; - the default setting specified by
style configure; or - the element's built-in default value for the option.
it's giving "who gets the aux cord" but for option values π
π LAYOUTS π
A layout specifies which elements make up a widget and how they're arranged. The layout engine uses a simplified version of the pack algorithm: you start with an initial cavity equal to the size of the widget, then elements get allocated a parcel within the cavity along the side specified by the -side option, and placed within the parcel according to the -sticky option. bestie it's feng shui for widgets π§
For example, the layout for a horizontal scrollbar is:
ttk::style layout Horizontal.TScrollbar {
Scrollbar.trough -children {
Scrollbar.leftarrow -side left -sticky w
Scrollbar.rightarrow -side right -sticky e
Scrollbar.thumb -sticky ew
}
}
By default, the layout for a widget is the same as its class name. Some widgets may override this β for example, the ttk::scrollbar widget chooses different layouts based on the -orient option. slay, flexible π
β¨ STATES β¨
In standard Tk, lots of widgets have a -state option which (in most cases) is either normal or disabled. Some widgets support additional states, like the entry widget which has a readonly state, and the various flavors of buttons which have an active state.
The themed Tk widgets generalize this idea: every widget has a bitmap of independent state flags. Widget state flags include active, disabled, pressed, focus, etc. β see ttk::widget(3tk) for the full list of state flags π
Instead of a -state option, every widget now has a state widget command which is used to set or query the state. A state specification is a list of symbolic state names indicating which bits are set, each optionally prefixed with an exclamation point indicating that the bit is cleared instead.
For example, the class bindings for the ttk::button widget are:
bind TButton <Enter>{ %W state active }
bind TButton <Leave>{ %W state !active }
bind TButton <Button-1>{ %W state pressed }
bind TButton <Button1-Leave>{ %W state !pressed }
bind TButton <Button1-Enter>{ %W state pressed }
bind TButton <ButtonRelease-1>\
{ %W instate {pressed} { %W state !pressed ; %W invoke } }
so what's this saying? the widget becomes active when the pointer enters the widget, and inactive when it leaves. similarly it becomes pressed when the mouse button is pressed, and !pressed on the ButtonRelease event. plus the button unpresses if the pointer gets dragged outside the widget while Button-1 is held down, and represses if it's dragged back in π
finally, when the mouse button is released, the widget's -command is invoked β but only if the button is currently in the pressed state.
(The actual bindings are a little more complicated than the above, but not by much.)
ngl that's honestly so real of them π
π STYLES π
Each widget is associated with a style, which specifies values for element options. Style names use a recursive dotted notation like layouts and elements; by default, widgets use the class name to look up a style in the current theme.
For example:
ttk::style configure TButton \
-background #d9d9d9 \
-foreground black \
-relief raised \
;
Many elements are displayed differently depending on the widget state. For example, buttons have a different background when they're active, a different foreground when disabled, and a different relief when pressed. The style map command specifies dynamic option settings for a particular style:
ttk::style map TButton \
-background [list disabled #d9d9d9 active #ececec] \
-foreground [list disabled #a3a3a3] \
-relief [list {pressed !disabled} sunken] \
;
glow-up on demand, literally π«
π¦ SEE ALSO π¦
ttk::widget(3tk)ttk::style(3tk)
and that's the whole vibe check, bestie π this page lives in the Tk source, version 8.5, and it's filed under ttk::intro(3tk). go forth and theme responsibly πβ¨π«Ά
π¦ where this page comes from
- debian trixie: tk9.0-doc 9.0.1-3
- ubuntu questing: tk9.0-doc 9.0.2-1
- ubuntu resolute: tk9.0-doc 9.0.3-1
- ubuntu stonking: tk9.0-doc 9.0.4-1
- Copyright (c) 2004 Joe English
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms