Gtk.StyleContext

g GObject.Object GObject.Object Gtk.StyleContext Gtk.StyleContext GObject.Object->Gtk.StyleContext

Subclasses:

None

Methods

Inherited:

GObject.Object (37)

Structs:

GObject.ObjectClass (5)

class

add_provider_for_display (display, provider, priority)

class

remove_provider_for_display (display, provider)

add_class (class_name)

add_provider (provider, priority)

get_border ()

get_color ()

get_display ()

get_margin ()

get_padding ()

get_scale ()

get_state ()

has_class (class_name)

lookup_color (color_name)

remove_class (class_name)

remove_provider (provider)

restore ()

save ()

set_display (display)

set_scale (scale)

set_state (flags)

to_string (flags)

Virtual Methods

Inherited:

GObject.Object (7)

do_changed ()

Properties

Name

Type

Flags

Short Description

display

Gdk.Display

r/w

Signals

Inherited:

GObject.Object (1)

Fields

Inherited:

GObject.Object (1)

Name

Type

Access

Description

parent_object

GObject.Object

r

Class Details

class Gtk.StyleContext(**kwargs)
Bases:

GObject.Object

Abstract:

No

Structure:

Gtk.StyleContextClass

GtkStyleContext stores styling information affecting a widget.

In order to construct the final style information, GtkStyleContext queries information from all attached GtkStyleProviders. Style providers can be either attached explicitly to the context through [method`Gtk`.StyleContext.add_provider], or to the display through [func`Gtk`.StyleContext.add_provider_for_display]. The resulting style is a combination of all providers’ information in priority order.

For GTK widgets, any GtkStyleContext returned by [method`Gtk`.Widget.get_style_context] will already have a GdkDisplay and RTL/LTR information set. The style context will also be updated automatically if any of these settings change on the widget.

Style Classes

Widgets can add style classes to their context, which can be used to associate different styles by class. The documentation for individual widgets lists which style classes it uses itself, and which style classes may be added by applications to affect their appearance.

Custom styling in UI libraries and applications

If you are developing a library with custom widgets that render differently than standard components, you may need to add a GtkStyleProvider yourself with the Gtk.STYLE_PROVIDER_PRIORITY_FALLBACK priority, either a GtkCssProvider or a custom object implementing the GtkStyleProvider interface. This way themes may still attempt to style your UI elements in a different way if needed so.

If you are using custom styling on an applications, you probably want then to make your style information prevail to the theme’s, so you must use a GtkStyleProvider with the Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION priority, keep in mind that the user settings in XDG_CONFIG_HOME/gtk-4.0/gtk.css will still take precedence over your changes, as it uses the Gtk.STYLE_PROVIDER_PRIORITY_USER priority.

Deprecated since version 4.10: The relevant API has been moved to [class`Gtk`.Widget] where applicable; otherwise, there is no replacement for querying the style machinery. Stylable UI elements should use widgets.

classmethod add_provider_for_display(display, provider, priority)[source]
Parameters:

Adds a global style provider to display, which will be used in style construction for all GtkStyleContexts under display.

GTK uses this to make styling information from GtkSettings available.

Note: If both priorities are the same, A GtkStyleProvider added through [method`Gtk`.StyleContext.add_provider] takes precedence over another added through this function.

classmethod remove_provider_for_display(display, provider)[source]
Parameters:

Removes provider from the global style providers list in display.

add_class(class_name)[source]
Parameters:

class_name (str) – class name to use in styling

Adds a style class to self, so later uses of the style context will make use of this new class for styling.

In the CSS file format, a GtkEntry defining a “search” class, would be matched by:

``css entry.search { … } ``

While any widget defining a “search” class would be matched by: ``css .search { … } ``

Deprecated since version 4.10: Use [method`Gtk`.Widget.add_css_class] instead

add_provider(provider, priority)[source]
Parameters:

Adds a style provider to self, to be used in style construction.

Note that a style provider added by this function only affects the style of the widget to which self belongs. If you want to affect the style of all widgets, use [func`Gtk`.StyleContext.add_provider_for_display].

Note: If both priorities are the same, a GtkStyleProvider added through this function takes precedence over another added through [func`Gtk`.StyleContext.add_provider_for_display].

Deprecated since version 4.10: Use style classes instead

get_border()[source]
Returns:

return value for the border settings

Return type:

border: Gtk.Border

Gets the border for a given state as a GtkBorder.

Deprecated since version 4.10: This api will be removed in GTK 5

get_color()[source]
Returns:

return value for the foreground color

Return type:

color: Gdk.RGBA

Gets the foreground color for a given state.

Deprecated since version 4.10: Use [method`Gtk`.Widget.get_color] instead

get_display()[source]
Returns:

a GdkDisplay.

Return type:

Gdk.Display

Returns the GdkDisplay to which self is attached.

Deprecated since version 4.10: Use [method`Gtk`.Widget.get_display] instead

get_margin()[source]
Returns:

return value for the margin settings

Return type:

margin: Gtk.Border

Gets the margin for a given state as a GtkBorder.

Deprecated since version 4.10: This api will be removed in GTK 5

get_padding()[source]
Returns:

return value for the padding settings

Return type:

padding: Gtk.Border

Gets the padding for a given state as a GtkBorder.

Deprecated since version 4.10: This api will be removed in GTK 5

get_scale()[source]
Returns:

the scale

Deprecated 4.10: Use [method`Gtk`.Widget.get_scale_factor] instead

Return type:

int

Returns the scale used for assets.

get_state()[source]
Returns:

the state flags

Return type:

Gtk.StateFlags

Returns the state used for style matching.

This method should only be used to retrieve the GtkStateFlags to pass to GtkStyleContext methods, like [method`Gtk`.StyleContext.get_padding]. If you need to retrieve the current state of a GtkWidget, use [method`Gtk`.Widget.get_state_flags].

Deprecated since version 4.10: Use [method`Gtk`.Widget.get_state_flags] instead

has_class(class_name)[source]
Parameters:

class_name (str) – a class name

Returns:

True if self has class_name defined

Return type:

bool

Returns True if self currently has defined the given class name.

Deprecated since version 4.10: Use [method`Gtk`.Widget.has_css_class] instead

lookup_color(color_name)[source]
Parameters:

color_name (str) – color name to lookup

Returns:

True if color_name was found and resolved, False otherwise

color:

Return location for the looked up color

Return type:

(bool, color: Gdk.RGBA)

Looks up and resolves a color name in the self color map.

Deprecated since version 4.10: This api will be removed in GTK 5

remove_class(class_name)[source]
Parameters:

class_name (str) – class name to remove

Removes class_name from self.

Deprecated since version 4.10: Use [method`Gtk`.Widget.remove_css_class] instead

remove_provider(provider)[source]
Parameters:

provider (Gtk.StyleProvider) – a GtkStyleProvider

Removes provider from the style providers list in self.

Deprecated since version 4.10.

restore()[source]

Restores self state to a previous stage.

See [method`Gtk`.StyleContext.save].

Deprecated since version 4.10: This API will be removed in GTK 5

save()[source]

Saves the self state.

This allows temporary modifications done through [method`Gtk`.StyleContext.add_class], [method`Gtk`.StyleContext.remove_class], [method`Gtk`.StyleContext.set_state] to be quickly reverted in one go through [method`Gtk`.StyleContext.restore].

The matching call to [method`Gtk`.StyleContext.restore] must be done before GTK returns to the main loop.

Deprecated since version 4.10: This API will be removed in GTK 5

set_display(display)[source]
Parameters:

display (Gdk.Display) – a GdkDisplay

Attaches self to the given display.

The display is used to add style information from “global” style providers, such as the display’s GtkSettings instance.

If you are using a GtkStyleContext returned from [method`Gtk`.Widget.get_style_context], you do not need to call this yourself.

Deprecated since version 4.10: You should not use this api

set_scale(scale)[source]
Parameters:

scale (int) – scale

Sets the scale to use when getting image assets for the style.

Deprecated since version 4.10: You should not use this api

set_state(flags)[source]
Parameters:

flags (Gtk.StateFlags) – state to represent

Sets the state to be used for style matching.

Deprecated since version 4.10: You should not use this api

to_string(flags)[source]
Parameters:

flags (Gtk.StyleContextPrintFlags) – Flags that determine what to print

Returns:

a newly allocated string representing self

Return type:

str

Converts the style context into a string representation.

The string representation always includes information about the name, state, id, visibility and style classes of the CSS node that is backing self. Depending on the flags, more information may be included.

This function is intended for testing and debugging of the CSS implementation in GTK. There are no guarantees about the format of the returned string, it may change.

Deprecated since version 4.10: This api will be removed in GTK 5

do_changed() virtual

Property Details

Gtk.StyleContext.props.display
Name:

display

Type:

Gdk.Display

Default Value:

None

Flags:

READABLE, WRITABLE