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_screen (screen, provider, priority)

class

new ()

class

remove_provider_for_screen (screen, provider)

class

reset_widgets (screen)

add_class (class_name)

add_provider (provider, priority)

add_region (region_name, flags)

cancel_animations (region_id)

get_background_color (state)

get_border (state)

get_border_color (state)

get_color (state)

get_direction ()

get_font (state)

get_frame_clock ()

get_junction_sides ()

get_margin (state)

get_padding (state)

get_parent ()

get_path ()

get_property (property, state)

get_scale ()

get_screen ()

get_section (property)

get_state ()

get_style_property (property_name, value)

has_class (class_name)

has_region (region_name)

invalidate ()

list_classes ()

list_regions ()

lookup_color (color_name)

lookup_icon_set (stock_id)

notify_state_change (window, region_id, state, state_value)

pop_animatable_region ()

push_animatable_region (region_id)

remove_class (class_name)

remove_provider (provider)

remove_region (region_name)

restore ()

save ()

scroll_animations (window, dx, dy)

set_background (window)

set_direction (direction)

set_frame_clock (frame_clock)

set_junction_sides (sides)

set_parent (parent)

set_path (path)

set_scale (scale)

set_screen (screen)

set_state (flags)

state_is_running (state)

to_string (flags)

Virtual Methods

Inherited:

GObject.Object (7)

do_changed ()

Properties

Name

Type

Flags

Short Description

direction

Gtk.TextDirection

d/r/w/en

Text direction deprecated

paint-clock

Gdk.FrameClock

r/w

The associated Gdk.FrameClock

parent

Gtk.StyleContext

r/w

The parent style context

screen

Gdk.Screen

r/w

The associated Gdk.Screen

Signals

Inherited:

GObject.Object (1)

Name

Short Description

changed

The ::changed signal is emitted when there is a change in the Gtk.StyleContext.

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

Gtk.StyleContext is an object that stores styling information affecting a widget defined by Gtk.WidgetPath.

In order to construct the final style information, Gtk.StyleContext queries information from all attached Gtk.StyleProviders. Style providers can be either attached explicitly to the context through Gtk.StyleContext.add_provider(), or to the screen through Gtk.StyleContext.add_provider_for_screen(). The resulting style is a combination of all providers’ information in priority order.

For GTK+ widgets, any Gtk.StyleContext returned by Gtk.Widget.get_style_context() will already have a Gtk.WidgetPath, a Gdk.Screen and RTL/LTR information set. The style context will also be updated automatically if any of these settings change on the widget.

If you are using the theming layer standalone, you will need to set a widget path and a screen yourself to the created style context through Gtk.StyleContext.set_path() and possibly Gtk.StyleContext.set_screen(). See the “Foreign drawing“ example in gtk3-demo.

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.

GTK+ defines macros for a number of style classes.

Style Regions

Widgets can also add regions with flags to their context. This feature is deprecated and will be removed in a future GTK+ update. Please use style classes instead.

GTK+ defines macros for a number of style regions.

Custom styling in UI libraries and applications

If you are developing a library with custom Gtk.Widgets that render differently than standard components, you may need to add a Gtk.StyleProvider yourself with the Gtk.STYLE_PROVIDER_PRIORITY_FALLBACK priority, either a Gtk.CssProvider or a custom object implementing the Gtk.StyleProvider 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 Gtk.StyleProvider with the Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION priority, keep in mind that the user settings in XDG_CONFIG_HOME/gtk-3.0/gtk.css will still take precedence over your changes, as it uses the Gtk.STYLE_PROVIDER_PRIORITY_USER priority.

classmethod add_provider_for_screen(screen, provider, priority)[source]
Parameters:

Adds a global style provider to screen, which will be used in style construction for all Gtk.StyleContexts under screen.

GTK+ uses this to make styling information from Gtk.Settings available.

Note: If both priorities are the same, A Gtk.StyleProvider added through Gtk.StyleContext.add_provider() takes precedence over another added through this function.

New in version 3.0.

classmethod new()[source]
Returns:

A newly created Gtk.StyleContext.

Return type:

Gtk.StyleContext

Creates a standalone Gtk.StyleContext, this style context won’t be attached to any widget, so you may want to call Gtk.StyleContext.set_path() yourself.

This function is only useful when using the theming layer separated from GTK+, if you are using Gtk.StyleContext to theme Gtk.Widgets, use Gtk.Widget.get_style_context() in order to get a style context ready to theme the widget.

classmethod remove_provider_for_screen(screen, provider)[source]
Parameters:

Removes provider from the global style providers list in screen.

New in version 3.0.

classmethod reset_widgets(screen)[source]
Parameters:

screen (Gdk.Screen) – a Gdk.Screen

This function recomputes the styles for all widgets under a particular Gdk.Screen. This is useful when some global parameter has changed that affects the appearance of all widgets, because when a widget gets a new style, it will both redraw and recompute any cached information about its appearance. As an example, it is used when the color scheme changes in the related Gtk.Settings object.

New in version 3.0.

add_class(class_name)[source]
Parameters:

class_name (str) – class name to use in styling

Adds a style class to self, so posterior calls to gtk_style_context_get() or any of the gtk_render_*() functions will make use of this new class for styling.

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

entry.search { ... }

While any widget defining a “search” class would be matched by:

.search { ... }

New in version 3.0.

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 Gtk.StyleContext.add_provider_for_screen().

Note: If both priorities are the same, a Gtk.StyleProvider added through this function takes precedence over another added through Gtk.StyleContext.add_provider_for_screen().

New in version 3.0.

add_region(region_name, flags)[source]
Parameters:
  • region_name (str) – region name to use in styling

  • flags (Gtk.RegionFlags) – flags that apply to the region

Adds a region to self, so posterior calls to gtk_style_context_get() or any of the gtk_render_*() functions will make use of this new region for styling.

In the CSS file format, a Gtk.TreeView defining a “row” region, would be matched by:

treeview row { ... }

Pseudo-classes are used for matching flags, so the two following rules:

treeview row:nth-child(even) { ... }
treeview row:nth-child(odd) { ... }

would apply to even and odd rows, respectively.

Region names must only contain lowercase letters and “-”, starting always with a lowercase letter.

New in version 3.0.

Deprecated since version 3.14.

cancel_animations(region_id)[source]
Parameters:

region_id (object or None) – animatable region to stop, or None. See Gtk.StyleContext.push_animatable_region()

Stops all running animations for region_id and all animatable regions underneath.

A None region_id will stop all ongoing animations in self, when dealing with a Gtk.StyleContext obtained through Gtk.Widget.get_style_context(), this is normally done for you in all circumstances you would expect all widget to be stopped, so this should be only used in complex widgets with different animatable regions.

New in version 3.0.

Deprecated since version 3.6: This function does nothing.

get_background_color(state)[source]
Parameters:

state (Gtk.StateFlags) – state to retrieve the color for

Returns:

return value for the background color

Return type:

color: Gdk.RGBA

Gets the background color for a given state.

This function is far less useful than it seems, and it should not be used in newly written code. CSS has no concept of “background color”, as a background can be an image, or a gradient, or any other pattern including solid colors.

The only reason why you would call Gtk.StyleContext.get_background_color() is to use the returned value to draw the background with it; the correct way to achieve this result is to use Gtk.render_background() instead, along with CSS style classes to modify the color to be rendered.

New in version 3.0.

Deprecated since version 3.16: Use Gtk.render_background() instead.

get_border(state)[source]
Parameters:

state (Gtk.StateFlags) – state to retrieve the border for

Returns:

return value for the border settings

Return type:

border: Gtk.Border

Gets the border for a given state as a Gtk.Border.

See Gtk.StyleContext.get_property() and Gtk.STYLE_PROPERTY_BORDER_WIDTH for details.

New in version 3.0.

get_border_color(state)[source]
Parameters:

state (Gtk.StateFlags) – state to retrieve the color for

Returns:

return value for the border color

Return type:

color: Gdk.RGBA

Gets the border color for a given state.

New in version 3.0.

Deprecated since version 3.16: Use Gtk.render_frame() instead.

get_color(state)[source]
Parameters:

state (Gtk.StateFlags) – state to retrieve the color for

Returns:

return value for the foreground color

Return type:

color: Gdk.RGBA

Gets the foreground color for a given state.

See Gtk.StyleContext.get_property() and Gtk.STYLE_PROPERTY_COLOR for details.

New in version 3.0.

get_direction()[source]
Returns:

the widget direction

Return type:

Gtk.TextDirection

Returns the widget direction used for rendering.

New in version 3.0.

Deprecated since version 3.8: Use Gtk.StyleContext.get_state() and check for Gtk.StateFlags.DIR_LTR and Gtk.StateFlags.DIR_RTL instead.

get_font(state)[source]
Parameters:

state (Gtk.StateFlags) – state to retrieve the font for

Returns:

the Pango.FontDescription for the given state. This object is owned by GTK+ and should not be freed.

Return type:

Pango.FontDescription

Returns the font description for a given state. The returned object is const and will remain valid until the Gtk.StyleContext ::changed signal happens.

New in version 3.0.

Deprecated since version 3.8: Use gtk_style_context_get() for “font” or subproperties instead.

get_frame_clock()[source]
Returns:

a Gdk.FrameClock, or None if self does not have an attached frame clock.

Return type:

Gdk.FrameClock or None

Returns the Gdk.FrameClock to which self is attached.

New in version 3.8.

get_junction_sides()[source]
Returns:

the junction sides

Return type:

Gtk.JunctionSides

Returns the sides where rendered elements connect visually with others.

New in version 3.0.

get_margin(state)[source]
Parameters:

state (Gtk.StateFlags) – state to retrieve the border for

Returns:

return value for the margin settings

Return type:

margin: Gtk.Border

Gets the margin for a given state as a Gtk.Border. See gtk_style_property_get() and Gtk.STYLE_PROPERTY_MARGIN for details.

New in version 3.0.

get_padding(state)[source]
Parameters:

state (Gtk.StateFlags) – state to retrieve the padding for

Returns:

return value for the padding settings

Return type:

padding: Gtk.Border

Gets the padding for a given state as a Gtk.Border. See gtk_style_context_get() and Gtk.STYLE_PROPERTY_PADDING for details.

New in version 3.0.

get_parent()[source]
Returns:

the parent context or None

Return type:

Gtk.StyleContext or None

Gets the parent context set via Gtk.StyleContext.set_parent(). See that function for details.

New in version 3.4.

get_path()[source]
Returns:

A Gtk.WidgetPath

Return type:

Gtk.WidgetPath

Returns the widget path used for style matching.

New in version 3.0.

get_property(property, state)[source]
Parameters:
  • property (str) – style property name

  • state (Gtk.StateFlags) – state to retrieve the property value for

Returns:

return location for the style property value

Return type:

value: GObject.Value

Gets a style property from self for the given state.

Note that not all CSS properties that are supported by GTK+ can be retrieved in this way, since they may not be representable as GObject.Value. GTK+ defines macros for a number of properties that can be used with this function.

Note that passing a state other than the current state of self is not recommended unless the style context has been saved with Gtk.StyleContext.save().

When value is no longer needed, GObject.Value.unset() must be called to free any allocated memory.

New in version 3.0.

get_scale()[source]
Returns:

the scale

Return type:

int

Returns the scale used for assets.

New in version 3.10.

get_screen()[source]
Returns:

a Gdk.Screen.

Return type:

Gdk.Screen

Returns the Gdk.Screen to which self is attached.

get_section(property)[source]
Parameters:

property (str) – style property name

Returns:

None or the section where a value for property was defined

Return type:

Gtk.CssSection or None

Queries the location in the CSS where property was defined for the current self. Note that the state to be queried is taken from Gtk.StyleContext.get_state().

If the location is not available, None will be returned. The location might not be available for various reasons, such as the property being overridden, property not naming a supported CSS property or tracking of definitions being disabled for performance reasons.

Shorthand CSS properties cannot be queried for a location and will always return None.

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 Gtk.StateFlags to pass to Gtk.StyleContext methods, like Gtk.StyleContext.get_padding(). If you need to retrieve the current state of a Gtk.Widget, use Gtk.Widget.get_state_flags().

New in version 3.0.

get_style_property(property_name, value)[source]
Parameters:
  • property_name (str) – the name of the widget style property

  • value (GObject.Value) – Return location for the property value

Gets the value for a widget style property.

When value is no longer needed, GObject.Value.unset() must be called to free any allocated memory.

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.

New in version 3.0.

has_region(region_name)[source]
Parameters:

region_name (str) – a region name

Returns:

True if region is defined

flags_return:

return location for region flags

Return type:

(bool, flags_return: Gtk.RegionFlags)

Returns True if self has the region defined. If flags_return is not None, it is set to the flags affecting the region.

New in version 3.0.

Deprecated since version 3.14.

invalidate()[source]

Invalidates self style information, so it will be reconstructed again. It is useful if you modify the self and need the new information immediately.

New in version 3.0.

Deprecated since version 3.12: Style contexts are invalidated automatically.

list_classes()[source]
Returns:

a GLib.List of strings with the currently defined classes. The contents of the list are owned by GTK+, but you must free the list itself with g_list_free() when you are done with it.

Return type:

[str]

Returns the list of classes currently defined in self.

New in version 3.0.

list_regions()[source]
Returns:

a GLib.List of strings with the currently defined regions. The contents of the list are owned by GTK+, but you must free the list itself with g_list_free() when you are done with it.

Return type:

[str]

Returns the list of regions currently defined in self.

New in version 3.0.

Deprecated since version 3.14.

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.

lookup_icon_set(stock_id)[source]
Parameters:

stock_id (str) – an icon name

Returns:

The looked up Gtk.IconSet, or None

Return type:

Gtk.IconSet or None

Looks up stock_id in the icon factories associated to self and the default icon factory, returning an icon set if found, otherwise None.

Deprecated since version 3.10: Use Gtk.IconTheme.lookup_icon() instead.

notify_state_change(window, region_id, state, state_value)[source]
Parameters:

Notifies a state change on self, so if the current style makes use of transition animations, one will be started so all rendered elements under region_id are animated for state state being set to value state_value.

The window parameter is used in order to invalidate the rendered area as the animation runs, so make sure it is the same window that is being rendered on by the gtk_render_*() functions.

If region_id is None, all rendered elements using self will be affected by this state transition.

As a practical example, a Gtk.Button notifying a state transition on the prelight state:

gtk_style_context_notify_state_change (context,
gtk_widget_get_window (widget),
NULL,
GTK_STATE_PRELIGHT,
button->in_button);

Can be handled in the CSS file like this:

button {
    background-color: #f00
}

button:hover {
    background-color: #fff;
    transition: 200ms linear
}

This combination will animate the button background from red to white if a pointer enters the button, and back to red if the pointer leaves the button.

Note that state is used when finding the transition parameters, which is why the style places the transition under the :hover pseudo-class.

New in version 3.0.

Deprecated since version 3.6: This function does nothing.

pop_animatable_region()[source]

Pops an animatable region from self. See Gtk.StyleContext.push_animatable_region().

New in version 3.0.

Deprecated since version 3.6: This function does nothing.

push_animatable_region(region_id)[source]
Parameters:

region_id (object or None) – unique identifier for the animatable region

Pushes an animatable region, so all further gtk_render_*() calls between this call and the following Gtk.StyleContext.pop_animatable_region() will potentially show transition animations for this region if Gtk.StyleContext.notify_state_change() is called for a given state, and the current theme/style defines transition animations for state changes.

The region_id used must be unique in self so the themes can uniquely identify rendered elements subject to a state transition.

New in version 3.0.

Deprecated since version 3.6: This function does nothing.

remove_class(class_name)[source]
Parameters:

class_name (str) – class name to remove

Removes class_name from self.

New in version 3.0.

remove_provider(provider)[source]
Parameters:

provider (Gtk.StyleProvider) – a Gtk.StyleProvider

Removes provider from the style providers list in self.

New in version 3.0.

remove_region(region_name)[source]
Parameters:

region_name (str) – region name to unset

Removes a region from self.

New in version 3.0.

Deprecated since version 3.14.

restore()[source]

Restores self state to a previous stage. See Gtk.StyleContext.save().

New in version 3.0.

save()[source]

Saves the self state, so temporary modifications done through Gtk.StyleContext.add_class(), Gtk.StyleContext.remove_class(), Gtk.StyleContext.set_state(), etc. can quickly be reverted in one go through Gtk.StyleContext.restore().

The matching call to Gtk.StyleContext.restore() must be done before GTK returns to the main loop.

New in version 3.0.

scroll_animations(window, dx, dy)[source]
Parameters:

This function is analogous to Gdk.Window.scroll(), and should be called together with it so the invalidation areas for any ongoing animation are scrolled together with it.

New in version 3.0.

Deprecated since version 3.6: This function does nothing.

set_background(window)[source]
Parameters:

window (Gdk.Window) – a Gdk.Window

Sets the background of window to the background pattern or color specified in self for its current state.

New in version 3.0.

Deprecated since version 3.18: Use Gtk.render_background() instead. Note that clients still using this function are now responsible for calling this function again whenever context is invalidated.

set_direction(direction)[source]
Parameters:

direction (Gtk.TextDirection) – the new direction.

Sets the reading direction for rendering purposes.

If you are using a Gtk.StyleContext returned from Gtk.Widget.get_style_context(), you do not need to call this yourself.

New in version 3.0.

Deprecated since version 3.8: Use Gtk.StyleContext.set_state() with Gtk.StateFlags.DIR_LTR and Gtk.StateFlags.DIR_RTL instead.

set_frame_clock(frame_clock)[source]
Parameters:

frame_clock (Gdk.FrameClock) – a Gdk.FrameClock

Attaches self to the given frame clock.

The frame clock is used for the timing of animations.

If you are using a Gtk.StyleContext returned from Gtk.Widget.get_style_context(), you do not need to call this yourself.

New in version 3.8.

set_junction_sides(sides)[source]
Parameters:

sides (Gtk.JunctionSides) – sides where rendered elements are visually connected to other elements

Sets the sides where rendered elements (mostly through Gtk.render_frame()) will visually connect with other visual elements.

This is merely a hint that may or may not be honored by themes.

Container widgets are expected to set junction hints as appropriate for their children, so it should not normally be necessary to call this function manually.

New in version 3.0.

set_parent(parent)[source]
Parameters:

parent (Gtk.StyleContext or None) – the new parent or None

Sets the parent style context for self. The parent style context is used to implement inheritance of properties.

If you are using a Gtk.StyleContext returned from Gtk.Widget.get_style_context(), the parent will be set for you.

New in version 3.4.

set_path(path)[source]
Parameters:

path (Gtk.WidgetPath) – a Gtk.WidgetPath

Sets the Gtk.WidgetPath used for style matching. As a consequence, the style will be regenerated to match the new given path.

If you are using a Gtk.StyleContext returned from Gtk.Widget.get_style_context(), you do not need to call this yourself.

New in version 3.0.

set_scale(scale)[source]
Parameters:

scale (int) – scale

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

New in version 3.10.

set_screen(screen)[source]
Parameters:

screen (Gdk.Screen) – a Gdk.Screen

Attaches self to the given screen.

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

If you are using a Gtk.StyleContext returned from Gtk.Widget.get_style_context(), you do not need to call this yourself.

New in version 3.0.

set_state(flags)[source]
Parameters:

flags (Gtk.StateFlags) – state to represent

Sets the state to be used for style matching.

New in version 3.0.

state_is_running(state)[source]
Parameters:

state (Gtk.StateType) – a widget state

Returns:

True if there is a running transition animation for state.

progress:

return location for the transition progress

Return type:

(bool, progress: float)

Returns True if there is a transition animation running for the current region (see Gtk.StyleContext.push_animatable_region()).

If progress is not None, the animation progress will be returned there, 0.0 means the state is closest to being unset, while 1.0 means it’s closest to being set. This means transition animation will run from 0 to 1 when state is being set and from 1 to 0 when it’s being unset.

New in version 3.0.

Deprecated since version 3.6: This function always returns False

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.

New in version 3.20.

do_changed() virtual

Signal Details

Gtk.StyleContext.signals.changed(style_context)
Signal Name:

changed

Flags:

RUN_FIRST

Parameters:

style_context (Gtk.StyleContext) – The object which received the signal

The ::changed signal is emitted when there is a change in the Gtk.StyleContext.

For a Gtk.StyleContext returned by Gtk.Widget.get_style_context(), the Gtk.Widget ::style-updated signal/vfunc might be more convenient to use.

This signal is useful when using the theming layer standalone.

New in version 3.0.

Property Details

Gtk.StyleContext.props.direction
Name:

direction

Type:

Gtk.TextDirection

Default Value:

Gtk.TextDirection.LTR

Flags:

DEPRECATED, READABLE, WRITABLE, EXPLICIT_NOTIFY

Text direction

Deprecated since version ???.

Gtk.StyleContext.props.paint_clock
Name:

paint-clock

Type:

Gdk.FrameClock

Default Value:

None

Flags:

READABLE, WRITABLE

The associated Gdk.FrameClock

Gtk.StyleContext.props.parent
Name:

parent

Type:

Gtk.StyleContext

Default Value:

None

Flags:

READABLE, WRITABLE

Sets or gets the style context’s parent. See Gtk.StyleContext.set_parent() for details.

New in version 3.4.

Gtk.StyleContext.props.screen
Name:

screen

Type:

Gdk.Screen

Default Value:

None

Flags:

READABLE, WRITABLE

The associated Gdk.Screen