ttk::intro(3tk)

πŸ“¦
version 9.0.4 Β· 2 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– 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:

  1. an option of the same name and type in the widget containing the element;
  2. a dynamic setting specified by style map and the current state;
  3. the default setting specified by style configure; or
  4. 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