Skip to content

Gtk.CssProvider

class — extends GObject.Object, StyleProvider

A style provider for CSS.

It is able to parse CSS-like input in order to style widgets.

An application can make GTK parse a specific CSS style sheet by calling CssProvider.load_from_file or CssProvider.load_from_resource and adding the provider with StyleContext.add_provider or StyleContext.add_provider_for_display.

In addition, certain files will be read when GTK is initialized. First, the file $XDG_CONFIG_HOME/gtk-4.0/gtk.css is loaded if it exists. Then, GTK loads the first existing file among XDG_DATA_HOME/themes/THEME/gtk-VERSION/gtk-VARIANT.css, $HOME/.themes/THEME/gtk-VERSION/gtk-VARIANT.css, $XDG_DATA_DIRS/themes/THEME/gtk-VERSION/gtk-VARIANT.css and DATADIR/share/themes/THEME/gtk-VERSION/gtk-VARIANT.css, where THEME is the name of the current theme (see the Settings.gtk-theme-name setting), VARIANT is the variant to load (see the Settings.gtk-application-prefer-dark-theme setting), DATADIR is the prefix configured when GTK was compiled (unless overridden by the GTK_DATA_PREFIX environment variable), and VERSION is the GTK version number. If no file is found for the current version, GTK tries older versions all the way back to 4.0.

To track errors while loading CSS, connect to the CssProvider.parsing-error signal.

Constructors

new

@classmethod
def new(cls) -> CssProvider

Returns a newly created GtkCssProvider.

Methods

load_from_bytes

def load_from_bytes(self, data: bytes) -> None

Loads data into css_provider.

This clears any previously loaded information.

Parameters:

  • dataGBytes containing the data to load

load_from_data

def load_from_data(self, data: str, length: int) -> None

:::warning Deprecated since 4.12 This API is deprecated. :::

Loads data into css_provider.

This clears any previously loaded information.

Parameters:

  • data — CSS data to be parsed
  • length — the length of data in bytes, or -1 for NUL terminated strings

load_from_file

def load_from_file(self, file: Gio.File) -> None

Loads the data contained in file into css_provider.

This clears any previously loaded information.

Parameters:

  • fileGFile pointing to a file to load

load_from_path

def load_from_path(self, path: str | bytes | os.PathLike[str] | os.PathLike[bytes]) -> None

Loads the data contained in path into css_provider.

This clears any previously loaded information.

Parameters:

  • path — the path of a filename to load, in the GLib filename encoding

load_from_resource

def load_from_resource(self, resource_path: str) -> None

Loads the data contained in the resource at resource_path into the css_provider.

This clears any previously loaded information.

Parameters:

  • resource_path — a GResource resource path

load_from_string

def load_from_string(self, string: str) -> None

Loads string into css_provider.

This clears any previously loaded information.

Parameters:

  • string — the CSS to load

load_named

def load_named(self, name: str, variant: str | None = ...) -> None

:::warning Deprecated since 4.20 This API is deprecated. :::

Loads a theme from the usual theme paths.

The actual process of finding the theme might change between releases, but it is guaranteed that this function uses the same mechanism to load the theme that GTK uses for loading its own theme.

Parameters:

  • name — A theme name
  • variant — variant to load, for example, "dark", or None for the default

to_string

def to_string(self) -> str

Converts the provider into a string representation in CSS format.

Using CssProvider.load_from_string with the return value from this function on a new provider created with CssProvider.new will basically create a duplicate of this provider.

Properties

prefers_color_scheme

prefers_color_scheme: InterfaceColorScheme | int  # read/write

Define the color scheme used for rendering the user interface.

The UI can be set to either InterfaceColorScheme.LIGHT, or InterfaceColorScheme.DARK mode. Other values will be interpreted the same as InterfaceColorScheme.LIGHT.

This setting is be available for media queries in CSS:

@media (prefers-color-scheme: dark) {
  // some dark mode styling
}

Changing this setting will reload the style sheet.

prefers_contrast

prefers_contrast: InterfaceContrast | int  # read/write

Define the contrast mode to use for the user interface.

When set to InterfaceContrast.MORE or InterfaceContrast.LESS, the UI is rendered in high or low contrast.

When set to InterfaceContrast.NO_PREFERENCE (the default), the user interface will be rendered in default mode.

This setting is be available for media queries in CSS:

@media (prefers-contrast: more) {
  // some style with high contrast
}

Changing this setting will reload the style sheet.

prefers_reduced_motion

prefers_reduced_motion: ReducedMotion | int  # read/write

Define the type of reduced motion to use for the user interface.

When set to ReducedMotion.REDUCE the UI is rendered in with reduced motion animations.

When set to ReducedMotion.NO_PREFERENCE (the default), the user interface will be rendered in default mode.

This setting is be available for media queries in CSS:

@media (prefers-reduced-motion: reduce) {
  // some style with reduced motion
}

Changing this setting will reload the style sheet.

Signals

parsing-error

def on_parsing_error(self, section: CssSection, error: GLib.Error) -> None: ...

Signals that a parsing error occurred.

The expected error values are in the CssParserError and CssParserWarning enumerations.

The path, line and position describe the actual location of the error as accurately as possible.

Parsing errors are never fatal, so the parsing will resume after the error. Errors may however cause parts of the given data or even all of it to not be parsed at all. So it is a useful idea to check that the parsing succeeds by connecting to this signal.

Errors in the CssParserWarning enumeration should not be treated as fatal errors.

Note that this signal may be emitted at any time as the css provider may opt to defer parsing parts or all of the input to a later time than when a loading function was called.