Theming#
A theme is a named color scheme the engine turns into widget styles.
Theme declares a theme family in code from a handful of anchor colors;
ThemeDefinition is the per-mode container the engine consumes; and
Colors is the resolved palette you read off the active theme (through
Style.colors). install_legacy_themes brings the pre-2.0 theme names
back for migration. The Theming & Colors guide shows them in use.
Declaring a theme#
- class Theme(name, primary=None, success=None, info=None, warning=None, danger=None, secondary=None, neutral='#adb5bd', light=None, dark=None)#
Bases:
objectA semantic-anchor color theme family declared in code.
Declare the accent colors once (as the
[500]midpoint of each ramp) plus alightand/ordark{background, foreground}block; the family generates a well-contrastedThemeDefinitionper mode (<name>-light/<name>-dark). The per-mode ramp step for solids, borders, and inputs is chosen automatically – there is no per-mode shade boilerplate.- Parameters:
name (str) – Family name; generated variants are
<name>-light/<name>-dark.primary (str) – Accent
[500]anchor colors (hex).success (str) – Accent
[500]anchor colors (hex).info (str) – Accent
[500]anchor colors (hex).warning (str) – Accent
[500]anchor colors (hex).danger (str) – Accent
[500]anchor colors (hex).secondary (str) – Optional colored secondary accent (hex). When omitted,
secondaryderives from the neutral ramp.neutral (str) – Gray base for the neutral ramp (secondary, the light/dark accents).
light (dict) –
{'background': ..., 'foreground': ...}for that variant, or None to skip it.dark (dict) –
{'background': ..., 'foreground': ...}for that variant, or None to skip it.
- classmethod from_existing(base, *, name, **overrides)#
Derive a new family from an existing
Theme, overriding some tokens.Every token not overridden is inherited from
base, so a built-in can be re-branded by changing just itsprimary(and anything else).
- register()#
Register the generated variants on the live
Stylesingleton.Returns this theme so
theme = Theme(...).register()reads cleanly. RaisesRuntimeErrorif noStylehas been created yet.
- to_definitions()#
Return the generated per-mode
ThemeDefinitionobjects (light, then dark).A family that declares only one block yields a single definition.
- light: dict = None#
{'background': ..., 'foreground': ...}for the light variant, orNoneto skip it.
- class ThemeDefinition(name, colors, mode='light', **kwargs)#
Bases:
objectEncapsulates the name, color palette, and metadata for a ttkbootstrap theme.
A ThemeDefinition is a lightweight container that pairs a theme name with its Colors object and whether it is a light or dark theme. The Style engine consumes ThemeDefinition instances to build widget styles and images for the active theme.
- Parameters:
- property type#
Deprecated alias for
mode(removed in 3.0).
The color palette#
- class Colors(primary, secondary, success, info, warning, danger, light, dark, bg, fg, selectbg, selectfg, border, inputfg, inputbg, active)#
Bases:
objectA class that defines the color scheme for a theme as well as provides several static methods for manipulating colors.
A
Colorsobject is attached to aThemeDefinitionand can also be accessed through theStyle.colorsproperty for the current theme.This class is an iterator over the main style color labels (primary, secondary, success, info, warning, danger, light, dark);
label_iteriterates over every theme color label.- Parameters:
primary (str) – The primary theme color; used by default for all widgets.
secondary (str) – An accent color; commonly of a
greyhue.success (str) – An accent color; commonly of a
greenhue.info (str) – An accent color; commonly of a
bluehue.warning (str) – An accent color; commonly of an
orangehue.danger (str) – An accent color; commonly of a
redhue.light (str) – An accent color.
dark (str) – An accent color.
bg (str) – Background color.
fg (str) – Default text color.
selectfg (str) – The color of selected text.
selectbg (str) – The background color of selected text.
border (str) – The color used for widget borders.
inputfg (str) – The text color for input widgets.
inputbg (str) – The text background color for input widgets.
active (str) – An accent color.
- static hex_to_rgb(color)#
Convert hexadecimal color to rgb color value
- Parameters:
color (str) – A hexadecimal color value
- Returns:
tuple[float, float, float] – The rgb color value, each channel normalized to 0-1.
- static label_iter()#
Iterate over all color label properties in the Color class
- Returns:
iter – An iterator for color label names
- static make_transparent(alpha, foreground, background='#ffffff')#
Simulate color transparency.
- static rgb_to_hex(r, g, b)#
Convert rgb to hexadecimal color value
- static rgb_to_hsv(r, g, b)#
Convert an rgb to hsv color value.
- static update_hsv(color, hd=0, sd=0, vd=0)#
Modify the hue, saturation, and/or value of a given hex color value by specifying the delta.
- get(color_label)#
Lookup a color value from the color name
- Parameters:
color_label (str) – A color label corresponding to a class property
- Returns:
str – A hexadecimal color value.
- get_contrast_ration(lum1, lum2)#
Calculate the contrast ratio between two luminance values.
- get_foreground(color_label)#
Return the appropriate foreground color for the specified color_label.
- get_luminance(color)#
Calculate the luminance of a color.
- Parameters:
color (str) – A hexadecimal color value.
- Returns:
float – The luminance value of the color.
- ramp(color_label)#
Return the full immutable 50-950 tint/shade ramp for a color role.
The role’s current value is treated as the
[500]anchor. Equivalent to reading each stop off the addressable color (self.get(label)[stop]) but returns the whole mapping at once.
- set(color_label, color_value)#
Set a color property value. This does not update any existing widgets. Can also be used to create on-demand color properties that can be used in your program after creation.
Legacy themes#
- install_legacy_themes(style=None)#
Register the pre-2.0 theme names on the live
Style(opt-in).Each legacy theme is adapted through
theme_from_legacy_dict(authored identity preserved, plumbing regenerated). Already-registered names are skipped, so calling this more than once is safe. Emits a one-timeDeprecationWarning: the legacy names are a migration convenience, not the 2.0 catalog.- Parameters:
style – The
Styleto register onto; defaults to the live singleton.- Return type:
None
See also#
Theming & Colors — how to pick, switch, and build themes, with examples.
Styling — the
Styleengine that consumes these themes.