pyclickimage.theme#

class pyclickimage.theme.Theme(name: str, dark: bool, window: str, surface: str, base: str, alternate_base: str, border: str, border_strong: str, text: str, text_muted: str, accent: str, accent_hover: str, accent_text: str, danger: str, warning: str, warning_hover: str, warning_text: str, plot_background: str, plot_grid: str, plot_minor_grid: str, plot_axis: str, plot_text: str, plot_line: str, plot_overlay: str, font_family: str = '', font_size: int = 10, plot_font_size: int = 8, radius: int = 6, padding: int = 5, splitter_width: int = 6)[source]#

Bases: object

Complete description of a visual theme.

All colors are CSS-like strings accepted by QColor ("#RRGGBB", "#AARRGGBB" or color names).

Parameters:
  • name (str) – Human-readable theme name.

  • dark (bool) – Whether the theme is dark. Used for a few contrast decisions.

  • window (str) – Background of windows and main surfaces.

  • surface (str) – Background of raised elements (toolbar, group boxes, menus).

  • base (str) – Background of input and content widgets (text edits, tables, combo boxes, plots).

  • alternate_base (str) – Alternate row color of tables and lists.

  • border (str) – Default border color.

  • border_strong (str) – Border color of hovered or focused elements.

  • text (str) – Main text color.

  • text_muted (str) – Secondary text color (labels, disabled text, placeholders).

  • accent (str) – Main accent color (selection, checked buttons, focus).

  • accent_hover (str) – Accent color under the mouse.

  • accent_text (str) – Text color displayed on top of accent.

  • danger (str) – Color used for destructive or error states.

  • warning (str) – Color used to flag a non-default state that needs attention (for instance a custom interpolation curve).

  • warning_hover (str) – Warning color under the mouse.

  • warning_text (str) – Text color displayed on top of warning.

  • plot_background (str) – Background of custom-painted plots.

  • plot_grid (str) – Major grid line color of plots.

  • plot_minor_grid (str) – Minor grid line color of plots.

  • plot_axis (str) – Axis and tick color of plots.

  • plot_text (str) – Tick label and axis title color of plots.

  • plot_line (str) – Data line, curve, node and histogram bar color.

  • plot_overlay (str) – Background of overlays drawn inside plots (for instance the mouse-coordinate box of QtLinearInterpolator).

  • font_family (str) – Application font family. An empty string keeps the system font.

  • font_size (int) – Application font size in points.

  • plot_font_size (int) – Font size used inside plots, in points.

  • radius (int) – Corner radius of buttons, inputs and frames, in pixels.

  • padding (int) – Base inner padding of buttons and inputs, in pixels.

  • splitter_width (int) – Width of splitter handles, in pixels.

Notes

The class is immutable. Use replace() to derive a new theme.

accent: str#
accent_hover: str#
accent_text: str#
alternate_base: str#
base: str#
border: str#
border_strong: str#
danger: str#
dark: bool#
font_family: str = ''#
font_size: int = 10#
classmethod from_dict(data: dict) → Theme[source]#

Create a theme from a dictionary.

Parameters:

data (dict) – Mapping field name -> value, as produced by to_dict(). Unknown keys are ignored.

Returns:

New theme instance.

Return type:

Theme

Raises:

TypeError – If a required field is missing.

name: str#
padding: int = 5#
plot_axis: str#
plot_background: str#
plot_font_size: int = 8#
plot_grid: str#
plot_line: str#
plot_minor_grid: str#
plot_overlay: str#
plot_text: str#
radius: int = 6#
replace(**changes) → Theme[source]#

Return a copy of the theme with some fields replaced.

Parameters:

**changes – Field names and their new values.

Returns:

New theme instance.

Return type:

Theme

Raises:

TypeError – If a key is not a field of Theme.

Examples

>>> compact = DARK_THEME.replace(font_size=9, padding=3)
splitter_width: int = 6#
surface: str#
text: str#
text_muted: str#
to_dict() → dict[source]#

Return the theme as a plain dictionary.

Returns:

Mapping field name -> value. Useful to save a theme to JSON; from_dict() performs the inverse operation.

Return type:

dict

warning: str#
warning_hover: str#
warning_text: str#
window: str#
pyclickimage.theme.get_theme(name: str) → Theme[source]#

Return a built-in theme by name.

Parameters:

name (str) – Theme name, case-insensitive ("dark" or "light").

Returns:

The requested theme.

Return type:

Theme

Raises:

KeyError – If no built-in theme has this name.

pyclickimage.theme.apply_theme(app: QApplication, theme: Theme = Theme(name='Light', dark=False, window='#f4f5f7', surface='#ffffff', base='#ffffff', alternate_base='#f7f8fa', border='#d9dde4', border_strong='#b3bac6', text='#1d2129', text_muted='#6b7280', accent='#2f6fe4', accent_hover='#4a84ec', accent_text='#ffffff', danger='#d64545', warning='#e8890c', warning_hover='#f59f2a', warning_text='#1d2129', plot_background='#ffffff', plot_grid='#e3e6eb', plot_minor_grid='#f0f2f5', plot_axis='#5f6672', plot_text='#4b5260', plot_line='#2f6fe4', plot_overlay='#dcffffff', font_family='', font_size=10, plot_font_size=8, radius=6, padding=5, splitter_width=6), extra_stylesheet: str = '', use_fusion: bool = True) → None[source]#

Apply a theme to a whole application.

Parameters:
  • app (QApplication) – Application to style.

  • theme (Theme, default=LIGHT_THEME) – Theme to apply.

  • extra_stylesheet (str, default="") – Additional QSS appended after the generated stylesheet. Rules defined here take precedence over the theme rules.

  • use_fusion (bool, default=True) – Whether to switch to the Fusion style, which gives a consistent look on every platform and fully honors palettes.

Notes

Call this function before creating the widgets when possible. Custom-painted plots (QtHistogram, QtProfilePlot, QtLinearInterpolator) store their colors when they are created and are not affected by stylesheets: restyle them with style_plot_widget() (or QtImageToolWindow.apply_theme()).

Examples

Apply the dark theme to a whole application:

from PyQt5 import QtWidgets
from theme import DARK_THEME, apply_theme

app = QtWidgets.QApplication([])
apply_theme(app, DARK_THEME)

Derive a custom theme from an existing one:

my_theme = DARK_THEME.replace(
    name="Dark green",
    accent="#3ecf8e",
    accent_hover="#5ee0a5",
)

Append custom rules to the generated stylesheet:

apply_theme(
    app,
    LIGHT_THEME,
    extra_stylesheet="QPushButton { font-weight: 600; }",
)
pyclickimage.theme.build_palette(theme: Theme) → QPalette[source]#

Build a QPalette from a theme.

The palette matters even with a stylesheet: widgets that paint themselves (and the default colors of the custom plots) read it.

Parameters:

theme (Theme) – Source theme.

Returns:

Palette for the active and inactive groups, with muted colors for the disabled group.

Return type:

QPalette

pyclickimage.theme.build_stylesheet(theme: Theme) → str[source]#

Generate the Qt stylesheet of a theme.

Parameters:

theme (Theme) – Source theme.

Returns:

Qt stylesheet (QSS) covering the standard widgets used by the application: windows, toolbars, buttons, inputs, combo boxes, check boxes, group boxes, tables, scroll bars, splitters, menus, tabs, sliders, status bars and tooltips.

Return type:

str

Notes

The stylesheet is a plain string: it can be edited, saved to a .qss file or extended before being applied with apply_theme().

pyclickimage.theme.style_plot_widget(widget: QWidget, theme: Theme = Theme(name='Light', dark=False, window='#f4f5f7', surface='#ffffff', base='#ffffff', alternate_base='#f7f8fa', border='#d9dde4', border_strong='#b3bac6', text='#1d2129', text_muted='#6b7280', accent='#2f6fe4', accent_hover='#4a84ec', accent_text='#ffffff', danger='#d64545', warning='#e8890c', warning_hover='#f59f2a', warning_text='#1d2129', plot_background='#ffffff', plot_grid='#e3e6eb', plot_minor_grid='#f0f2f5', plot_axis='#5f6672', plot_text='#4b5260', plot_line='#2f6fe4', plot_overlay='#dcffffff', font_family='', font_size=10, plot_font_size=8, radius=6, padding=5, splitter_width=6)) → None[source]#

Apply a theme to a custom-painted plot widget.

Supported widgets are QtHistogram, QtProfilePlot and QtLinearInterpolator. Their colors are stored in private attributes read by paintEvent; QSS does not reach them.

Parameters:
  • widget (QWidget) – Plot widget to restyle. Attributes the widget does not define are skipped, so any widget can be passed safely.

  • theme (Theme, default=LIGHT_THEME) – Theme to apply.

Notes

Histogram bars are drawn with a semi-transparent version of plot_line so that dense histograms stay readable.

The plot font size is set to theme.plot_font_size.

pyclickimage.theme.style_plot_widgets(widgets: Iterable[QWidget], theme: Theme = Theme(name='Light', dark=False, window='#f4f5f7', surface='#ffffff', base='#ffffff', alternate_base='#f7f8fa', border='#d9dde4', border_strong='#b3bac6', text='#1d2129', text_muted='#6b7280', accent='#2f6fe4', accent_hover='#4a84ec', accent_text='#ffffff', danger='#d64545', warning='#e8890c', warning_hover='#f59f2a', warning_text='#1d2129', plot_background='#ffffff', plot_grid='#e3e6eb', plot_minor_grid='#f0f2f5', plot_axis='#5f6672', plot_text='#4b5260', plot_line='#2f6fe4', plot_overlay='#dcffffff', font_family='', font_size=10, plot_font_size=8, radius=6, padding=5, splitter_width=6)) → None[source]#

Apply a theme to several custom-painted plot widgets.

Parameters:
  • widgets (iterable of QWidget) – Plot widgets to restyle.

  • theme (Theme, default=LIGHT_THEME) – Theme to apply.

pyclickimage.theme.DARK_THEME#

modern dark theme (default).

Type:

Theme

pyclickimage.theme.LIGHT_THEME#

modern light theme.

Type:

Theme

pyclickimage.theme.THEMES#

Built-in themes indexed by lower-case name.

Type:

dict[str, Theme]