Skip to content

Style

Bases: Style

Enhanced TTK Style with builder registry and theme management.

This class extends ttk.Style to provide:

  • Singleton pattern (one instance per Tkinter master)
  • Integration with BootstyleBuilder registry
  • Theme management via ThemeProvider
  • Automatic style rebuilding on theme changes
  • Custom style options support

The Style class maintains registries to track:

  • All created styles (for theme change rebuilds)
  • Which styles exist in each theme (for caching)
  • Custom options per style (for recreating with same options)

style_builder property

style_builder: BootstyleBuilderTTk

Get the builder manager instance.

Returns:

Type Description
BootstyleBuilderTTk

BootstyleBuilder instance

theme_provider property

theme_provider: ThemeProvider

Get the theme provider instance.

Returns:

Type Description
ThemeProvider

ThemeProvider instance

current_theme property

current_theme: Optional[str]

Get the current theme name.

Returns:

Type Description
Optional[str]

Current theme name or None

colors property

colors: dict[str, str]

Get colors for the current theme.

Returns:

Type Description
dict[str, str]

Colors dictionary from ThemeProvider

__new__

__new__(*args, **kwargs)

Ensure Style() always returns the global singleton instance.

If an instance already exists, return it. Otherwise, create it.

__init__

__init__(master: Master = None, theme: str = 'light')

Initialize the Style instance.

Parameters:

Name Type Description Default
master Master

Tkinter master widget (None for default)

None
theme str

Initial theme name (default: "light")

'light'

list_themes

list_themes() -> list[dict[str, str]]

Return available themes as [{'name', 'display_name'}, ...].

This delegates to the underlying ThemeProvider. Themes are always loaded from both the v2 and legacy theme packages.

Returns:

Type Description
list[dict[str, str]]

List of theme dictionaries with 'name' and 'display_name' keys.

style_exists

style_exists(style: str) -> bool

Check if a style exists (basic check).

Parameters:

Name Type Description Default
style str

TTK style name

required

Returns:

Type Description
bool

True if style has any configuration

register_style

register_style(
    ttk_style: str, options: Optional[dict] = None
)

Register a style in the current theme.

This adds the style to registries so it can be:

  • Cached (not recreated if already exists)
  • Rebuilt when theme changes
  • Recreated with same options

Parameters:

Name Type Description Default
ttk_style str

Full TTK style name

required
options Optional[dict]

Optional custom style options

None

create_style

create_style(
    widget_class: str,
    variant: str,
    ttk_style: str,
    accent: Optional[str] = None,
    options: Optional[dict] = None,
) -> None

Create a new style if it doesn't exist in current theme.

Parameters:

Name Type Description Default
widget_class str

TTK widget class (e.g., "TButton")

required
variant str

Variant name (e.g., "outline")

required
ttk_style str

Full TTK style name (e.g., "success.Outline.TButton")

required
accent Optional[str]

Optional accent token (e.g., "success", "blue[100]")

None
options Optional[dict]

Optional custom style options

None

theme_use

theme_use(name: str = None) -> str | None

Switch to a different theme and rebuild all styles.

Applies theme change to the global Style instance, rebuilds all TTK styles and registered Tk widgets, and publishes a legacy theme-change event for subscribers.

Parameters:

Name Type Description Default
name str

Theme name to switch to. If None, returns the current theme.

None

Returns:

Type Description
str | None

The current theme name, or None if no theme is set.

register_tk_widget

register_tk_widget(widget) -> None

Register a Tk widget to be restyled on theme changes.

get_style_builder

get_style_builder() -> BootstyleBuilderTTk

Get the style builder instance.

Returns:

Type Description
BootstyleBuilderTTk

BootstyleBuilder instance

__repr__

__repr__() -> str

String representation of Style instance.