StyleContext#
Deprecated since version 4.10:
- The relevant API has been moved to
Widget where applicable; otherwise, there is no replacement for querying the style machinery. Stylable UI elements should use widgets.
Superclasses: Object
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
add_provider, or to the display through
add_provider_for_display. The resulting
style is a combination of all providers’ information in priority order.
For GTK widgets, any GtkStyleContext returned by
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 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 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
STYLE_PROVIDER_PRIORITY_USER priority.
Methods#
- class StyleContext
- add_class(class_name: str) None#
Adds a style class to
context, so later uses of the style context will make use of this new class for styling.In the CSS file format, a
GtkEntrydefining a “search” class, would be matched by:entry.search { ... }
While any widget defining a “search” class would be matched by:
.search { ... }
Deprecated since version 4.10: Use
add_css_classinstead- Parameters:
class_name – class name to use in styling
- add_provider(provider: StyleProvider, priority: int) None#
Adds a style provider to
context, to be used in style construction.Note that a style provider added by this function only affects the style of the widget to which
contextbelongs. If you want to affect the style of all widgets, useadd_provider_for_display.Note: If both priorities are the same, a
GtkStyleProvideradded through this function takes precedence over another added throughadd_provider_for_display.Deprecated since version 4.10: Use style classes instead
- Parameters:
provider – a
GtkStyleProviderpriority – the priority of the style provider. The lower it is, the earlier it will be used in the style construction. Typically this will be in the range between
STYLE_PROVIDER_PRIORITY_FALLBACKandSTYLE_PROVIDER_PRIORITY_USER
- classmethod add_provider_for_display(provider: StyleProvider, priority: int) None#
Adds a global style provider to
display, which will be used in style construction for allGtkStyleContextsunderdisplay.GTK uses this to make styling information from
GtkSettingsavailable.Note: If both priorities are the same, A
GtkStyleProvideradded throughadd_providertakes precedence over another added through this function.- Parameters:
provider – a
GtkStyleProviderpriority – the priority of the style provider. The lower it is, the earlier it will be used in the style construction. Typically this will be in the range between
STYLE_PROVIDER_PRIORITY_FALLBACKandSTYLE_PROVIDER_PRIORITY_USER
- get_border() 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() RGBA#
Gets the foreground color for a given state.
Deprecated since version 4.10: Use
get_colorinstead
- get_display() Display#
Returns the
GdkDisplayto whichcontextis attached.Deprecated since version 4.10: Use
get_displayinstead
- get_margin() 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() 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() int#
Returns the scale used for assets.
Deprecated since version 4.10: Use
get_scale_factorinstead
- get_state() StateFlags#
Returns the state used for style matching.
This method should only be used to retrieve the
GtkStateFlagsto pass toGtkStyleContextmethods, likeget_padding. If you need to retrieve the current state of aGtkWidget, useget_state_flags.Deprecated since version 4.10: Use
get_state_flagsinstead
- has_class(class_name: str) bool#
Returns
Trueifcontextcurrently has defined the given class name.Deprecated since version 4.10: Use
has_css_classinstead- Parameters:
class_name – a class name
- lookup_color(color_name: str) tuple[bool, RGBA]#
Looks up and resolves a color name in the
contextcolor map.Deprecated since version 4.10: This api will be removed in GTK 5
- Parameters:
color_name – color name to lookup
- remove_class(class_name: str) None#
Removes
class_namefromcontext.Deprecated since version 4.10: Use
remove_css_classinstead- Parameters:
class_name – class name to remove
- remove_provider(provider: StyleProvider) None#
Removes
providerfrom the style providers list incontext.Deprecated since version 4.10: Please do not use it in newly written code
- Parameters:
provider – a
GtkStyleProvider
- classmethod remove_provider_for_display(provider: StyleProvider) None#
Removes
providerfrom the global style providers list indisplay.- Parameters:
provider – a
GtkStyleProvider
- restore() None#
Restores
contextstate to a previous stage.See
save.Deprecated since version 4.10: This API will be removed in GTK 5
- save() None#
Saves the
contextstate.This allows temporary modifications done through
add_class,remove_class,set_stateto be quickly reverted in one go throughrestore.The matching call to
restoremust 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: Display) None#
Attaches
contextto the given display.The display is used to add style information from “global” style providers, such as the display’s
GtkSettingsinstance.If you are using a
GtkStyleContextreturned fromget_style_context, you do not need to call this yourself.Deprecated since version 4.10: You should not use this api
- Parameters:
display – a
GdkDisplay
- set_scale(scale: int) None#
Sets the scale to use when getting image assets for the style.
Deprecated since version 4.10: You should not use this api
- Parameters:
scale – scale
- set_state(flags: StateFlags) None#
Sets the state to be used for style matching.
Deprecated since version 4.10: You should not use this api
- Parameters:
flags – state to represent
- to_string(flags: StyleContextPrintFlags) 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
context. 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
- Parameters:
flags – Flags that determine what to print
Properties#
Virtual Methods#
Fields#
- class StyleContext
- parent_object#