ttkbootstrap Documentation

repository·master·Indexed 25 days ago

https://github.com/israel-dryer/ttkbootstrap

A theme extension for tkinter that provides modern, Bootstrap-inspired light and dark themes. It enables styling of ttk widgets using a canonical `bootstyle` grammar to describe intent (e.g., primary, success, danger) rather than manual color configuration. Version 2.0.1 includes a full set of Bootstrap Icons, runtime theme toggling, and a structured system for recolorable element assets and localization.

Tokens
165.8K
Snippets
308
Records
1.1K
Agent score
80%

What's inside ttkbootstrap

  1. Understand the ttkbootstrap 2.0 Unit Model

    master

    ttkbootstrap 2.0 uses three distinct measurement units to handle scaling and high-DPI displays correctly. Understanding these is critical when defining custom styles or working with assets:

    1. Logical UI units (lu): These are theme-authored dimensions at baseline density (e.g., padding, borders, radii, progressbar thickness). At 100% scaling, 1 lu = 1 px. These are NOT Tk points; font point sizes are handled separately by Tk.
    2. Physical pixels (px): Integer dimensions used for Pillow, PhotoImage, canvas coordinates, and pixel-valued style options. Warning: A value already in physical pixels must never be passed through scale_size again.
    3. Source-image pixels (spx): Coordinates within a source PNG/asset. These define the bitmap's resolution but do not define the UI widget size. Assets carry an explicit logical target size to map spx to lu.
  2. Use ttkbootstrap localization APIs

    master

    ttkbootstrap provides several tools to handle translations for built-in strings like dialog buttons and date names using Tk's message catalog:

    • L(): Performs a lookup for a translated string.
    • set_locale(): Switches the active locale for the application.
    • LocaleVar: A specialized variable that automatically re-translates its content whenever the locale changes.
    • MessageCatalog: A lower-level API for managing the message catalog directly.
  3. Design principles for the ttkbootstrap 2.0 toolkit

    master

    The toolkit is designed as a set of thin helpers, not a Domain Specific Language (DSL).

    Key architectural principles:

    • Orthogonality: Each helper performs a single mechanical job and composes with raw ttk calls.
    • Key Derivation: Cache keys are computed from the inputs that determine pixels. It is impossible to write a draw whose key omits a color it uses, as the key is built internally from the recipe or the *key_parts provided in the image() call.
    • No Internal State: The toolkit adds no runtime state of its own beyond the existing Style._image_cache.
  4. Visual language for check, radio, and switch indicators

    master

    In ttkbootstrap 2.0, checkbuttons, radiobuttons, and switches use a flat, single-glyph aesthetic. Instead of two-tone filled looks, they use one glyph per state rendered in the state color.

    For glyphs with interior marks (like -fill variants), the mark is a transparent knockout that shows the widget background through it. This ensures the indicators are automatically theme-adaptive (e.g., a primary square with a light-background check on light themes, and a dark-background check on dark themes).

  5. Use Bootstrap Icons in ttkbootstrap

    master

    ttkbootstrap uses the Bootstrap Icons font to render glyph-shaped widget assets (such as checkbutton/radiobutton/switch indicators, combobox/spinbox arrows, date-entry buttons, and sizegrips).

    Because the icons are bundled directly within the package, there are no extra pip dependencies required to use them.

    Key components:

    • ttkbootstrap.Icon: The primary atom for icon usage.
    • ttkbootstrap.style.icon_element: A per-state sugar utility for icon styling.
  6. API Normalization and Compatibility in ttkbootstrap 2.0

    master

    In version 2.0, ttkbootstrap is undergoing API normalization for several core widgets (Window, Toplevel, dialogs, and Tableview). To prevent breaking existing code, the project uses a Hybrid posture:

    • High-value surfaces (Window, Toplevel, and dialogs) are being normalized with warn-and-normalize aliases via style/_compat.py. These aliases will provide warnings during 2.x and will be removed in 3.0.
    • Tableview is receiving bug fixes, dead code removal, and re-exports, but established method names are being preserved for now to avoid massive documentation churn. A separate migration for method renaming is planned for a later release.
  7. Explore ttkbootstrap Feature Guides

    master

    Use the feature guides for deep dives into specific subsystems:

    • Typography: Managing Fonts and global families via set_global_family.
    • Localization: Implementing live language switching using L(), LocaleVar, and set_locale.
    • Input Validation: Using add_*_validation helpers and the @validator decorator.
    • Icons: Using theme-aware Bootstrap Icons via the icon= keyword, apply_icon, or the Icon class.
    • Windows & Menus: Setting up App/Toplevel windows and creating complex menu bars/submenus.
    • Dialogs: Using Messagebox, Querybox, date/font/color pickers, and filedialog.
    • Theming: Choosing themes, switching between light/dark modes, and accessing theme colors via style.colors.
  8. Visual changes to Scrollbars and Buttons in 2.0

    master

    Users of version 2.0 will notice several visual updates that do not require code changes:

    • Scrollbars: The default variant is now a flat square thumb, while round is a pill shape. Both feature a visible trough and an inset thumb with a minimum length to prevent them from disappearing in long lists.
    • Buttons: Solid buttons now feature a subtle 1px hairline border derived from the button's fill color to ensure they are visible against similar backgrounds.
    • Toolbuttons & Switches: The 'OFF' state for Toolbuttons and Switches is now more visually quiet, using a muted surface raise and de-emphasized text to ensure the 'ON' (selected) state carries the primary emphasis.
  9. Understand the 2.0 asset caching and styling architecture

    master

    The 2.0 architecture uses a content-addressed image cache to ensure efficient rendering and deduplication across themes. When working with assets, keep the following architectural constraints in mind:

    • Content-Addressed Cache: The engine uses Style._get_or_create_image(key, factory) and Style.clear_image_cache(). Cache keys are derived from pixel-determining inputs. Crucially, keys must never include the theme name; this allows identical assets to be deduplicated across different themes.
    • Asset Access: Use the Assets class in style/assets.py as the primary interface. It ensures cache keys are derived consistently from the same inputs used for rendering.
    • Styling Integration: Recolored assets are designed to plug directly into the existing layout system, including image_element, state_map/statespec, layout()/El, and register_style (which allows styles to be resolved via the style="..." attribute).
  10. Understand the ttkbootstrap architecture

    master

    ttkbootstrap is a styling extension for vanilla tkinter, not a replacement widget library.

    Key architectural principles:

    • Uses standard tkinter widgets: It themes existing widgets rather than providing new ones. All standard tkinter concepts (geometry managers, variables, events, widget tree) apply directly.
    • On-demand styling: Styles and themes are built only when used to minimize memory footprint.
    • Lightweight dependencies: The project only requires Pillow.
    • Performance: In version 2.0, assets are rendered once and shared, and theme switching is optimized to repaint only widgets currently on screen.