Gtk.CssProvider

g GObject.GInterface GObject.GInterface Gtk.StyleProvider Gtk.StyleProvider GObject.GInterface->Gtk.StyleProvider GObject.Object GObject.Object Gtk.CssProvider Gtk.CssProvider GObject.Object->Gtk.CssProvider Gtk.StyleProvider->Gtk.CssProvider

Subclasses:

None

Methods

Inherited:

GObject.Object (37)

Structs:

GObject.ObjectClass (5)

class

new ()

load_from_bytes (data)

load_from_data (data, length)

load_from_file (file)

load_from_path (path)

load_from_resource (resource_path)

load_from_string (string)

load_named (name, variant)

to_string ()

Virtual Methods

Inherited:

GObject.Object (7)

Properties

Name

Type

Flags

Short Description

prefers-color-scheme

Gtk.InterfaceColorScheme

r/w/en

prefers-contrast

Gtk.InterfaceContrast

r/w/en

Signals

Inherited:

GObject.Object (1), Gtk.StyleProvider (1)

Name

Short Description

parsing-error

Signals that a parsing error occurred.

Fields

Inherited:

GObject.Object (1), Gtk.StyleProvider (1)

Name

Type

Access

Description

parent_instance

GObject.Object

r

Class Details

class Gtk.CssProvider(**kwargs)
Bases:

GObject.Object, Gtk.StyleProvider

Abstract:

No

Structure:

Gtk.CssProviderClass

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 [method`Gtk`.CssProvider.load_from_file] or [method`Gtk`.CssProvider.load_from_resource] and adding the provider with [method`Gtk`.StyleContext.add_provider] or [func`Gtk`.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 [property`Gtk`.Settings:gtk-theme-name] setting), VARIANT is the variant to load (see the [property`Gtk`.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 [signal`Gtk`.CssProvider::parsing-error] signal.

classmethod new()[source]
Returns:

A new GtkCssProvider

Return type:

Gtk.CssProvider

Returns a newly created GtkCssProvider.

load_from_bytes(data)[source]
Parameters:

data (GLib.Bytes) – GBytes containing the data to load

Loads data into self.

This clears any previously loaded information.

New in version 4.12.

load_from_data(data, length)[source]
Parameters:
  • data (str) – CSS data to be parsed

  • length (int) – the length of data in bytes, or -1 for NUL terminated strings

Loads data into self.

This clears any previously loaded information.

Deprecated since version 4.12: Use [method`Gtk`.CssProvider.load_from_string] or [method`Gtk`.CssProvider.load_from_bytes] instead

load_from_file(file)[source]
Parameters:

file (Gio.File) – GFile pointing to a file to load

Loads the data contained in file into self.

This clears any previously loaded information.

load_from_path(path)[source]
Parameters:

path (str) – the path of a filename to load, in the GLib filename encoding

Loads the data contained in path into self.

This clears any previously loaded information.

load_from_resource(resource_path)[source]
Parameters:

resource_path (str) – a GResource resource path

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

This clears any previously loaded information.

load_from_string(string)[source]
Parameters:

string (str) – the CSS to load

Loads string into self.

This clears any previously loaded information.

New in version 4.12.

load_named(name, variant)[source]
Parameters:
  • name (str) – A theme name

  • variant (str or None) – variant to load, for example, “dark”, or None for the default

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.

Deprecated since version 4.20: Using any of the other theme loaders, combine with media queries.

to_string()[source]
Returns:

a new string representing the self.

Return type:

str

Converts the self into a string representation in CSS format.

Using [method`Gtk`.CssProvider.load_from_string] with the return value from this function on a new provider created with [ctor`Gtk`.CssProvider.new] will basically create a duplicate of this self.

Signal Details

Gtk.CssProvider.signals.parsing_error(css_provider, section, error)
Signal Name:

parsing-error

Flags:

RUN_LAST

Parameters:

Signals that a parsing error occurred.

The expected error values are in the [error`Gtk`.CssParserError] and [enum`Gtk`.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 [enum`Gtk`.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.

Property Details

Gtk.CssProvider.props.prefers_color_scheme
Name:

prefers-color-scheme

Type:

Gtk.InterfaceColorScheme

Default Value:

Gtk.InterfaceColorScheme.DEFAULT

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

Define the color scheme used for rendering the user interface.

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

This setting is be available for media queries in CSS:

``css @media (prefers-color-scheme: dark) {

// some dark mode styling

}

Changing this setting will cause a re-render of the style sheet.

New in version 4.20.

Gtk.CssProvider.props.prefers_contrast
Name:

prefers-contrast

Type:

Gtk.InterfaceContrast

Default Value:

Gtk.InterfaceContrast.NO_PREFERENCE

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

Define the contrast mode to use for the user interface.

When set to [enum`Gtk`.InterfaceContrast.MORE] or [enum`Gtk`.InterfaceContrast.LESS], the UI is rendered in high or low contrast.

When set to [enum`Gtk`.InterfaceContrast.NO_PREFERENCE] (the default), the user interface will be rendered in default mode.

This setting is be available for media queries in CSS:

``css @media (prefers-contrast: more) {

// some style with high contrast

}

Changing this setting will cause a re-render of the style sheet.

New in version 4.20.