:right-sidebar: True StyleContext =================================================================== .. currentmodule:: gi.repository.Gtk .. deprecated:: 4.10 The relevant API has been moved to :obj:`~gi.repository.Gtk.Widget` where applicable; otherwise, there is no replacement for querying the style machinery. Stylable UI elements should use widgets. .. class:: StyleContext(**properties: ~typing.Any) :no-contents-entry: Superclasses: :class:`~gi.repository.GObject.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 :obj:`~gi.repository.Gtk.StyleContext.add_provider`, or to the display through :obj:`~gi.repository.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 :obj:`~gi.repository.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 :const:`~gi.repository.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 :const:`~gi.repository.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 :const:`~gi.repository.Gtk.STYLE_PROVIDER_PRIORITY_USER` priority. Methods ------- .. rst-class:: interim-class .. class:: StyleContext :no-index: .. method:: 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 ``GtkEntry`` defining a “search” class, would be matched by: .. code-block:: :dedent: entry.search { ... } While any widget defining a “search” class would be matched by: .. code-block:: :dedent: .search { ... } .. deprecated:: 4.10 Use :obj:`~gi.repository.Gtk.Widget.add_css_class` instead :param class_name: class name to use in styling .. method:: add_provider(provider: ~gi.repository.Gtk.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 ``context`` belongs. If you want to affect the style of all widgets, use :obj:`~gi.repository.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 :obj:`~gi.repository.Gtk.StyleContext.add_provider_for_display`. .. deprecated:: 4.10 Use style classes instead :param provider: a ``GtkStyleProvider`` :param priority: 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 :const:`~gi.repository.Gtk.STYLE_PROVIDER_PRIORITY_FALLBACK` and :const:`~gi.repository.Gtk.STYLE_PROVIDER_PRIORITY_USER` .. method:: add_provider_for_display(display: ~gi.repository.Gdk.Display, provider: ~gi.repository.Gtk.StyleProvider, priority: int) -> None 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 :obj:`~gi.repository.Gtk.StyleContext.add_provider` takes precedence over another added through this function. :param display: a ``GdkDisplay`` :param provider: a ``GtkStyleProvider`` :param priority: 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 :const:`~gi.repository.Gtk.STYLE_PROVIDER_PRIORITY_FALLBACK` and :const:`~gi.repository.Gtk.STYLE_PROVIDER_PRIORITY_USER` .. method:: get_border() -> ~gi.repository.Gtk.Border Gets the border for a given state as a ``GtkBorder``. .. deprecated:: 4.10 This api will be removed in GTK 5 .. method:: get_color() -> ~gi.repository.Gdk.RGBA Gets the foreground color for a given state. .. deprecated:: 4.10 Use :obj:`~gi.repository.Gtk.Widget.get_color` instead .. method:: get_display() -> ~gi.repository.Gdk.Display Returns the ``GdkDisplay`` to which ``context`` is attached. .. deprecated:: 4.10 Use :obj:`~gi.repository.Gtk.Widget.get_display` instead .. method:: get_margin() -> ~gi.repository.Gtk.Border Gets the margin for a given state as a ``GtkBorder``. .. deprecated:: 4.10 This api will be removed in GTK 5 .. method:: get_padding() -> ~gi.repository.Gtk.Border Gets the padding for a given state as a ``GtkBorder``. .. deprecated:: 4.10 This api will be removed in GTK 5 .. method:: get_scale() -> int Returns the scale used for assets. .. deprecated:: 4.10 Use :obj:`~gi.repository.Gtk.Widget.get_scale_factor` instead .. method:: get_state() -> ~gi.repository.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 :obj:`~gi.repository.Gtk.StyleContext.get_padding`. If you need to retrieve the current state of a ``GtkWidget``, use :obj:`~gi.repository.Gtk.Widget.get_state_flags`. .. deprecated:: 4.10 Use :obj:`~gi.repository.Gtk.Widget.get_state_flags` instead .. method:: has_class(class_name: str) -> bool Returns :const:`True` if ``context`` currently has defined the given class name. .. deprecated:: 4.10 Use :obj:`~gi.repository.Gtk.Widget.has_css_class` instead :param class_name: a class name .. method:: lookup_color(color_name: str) -> tuple[bool, ~gi.repository.Gdk.RGBA] Looks up and resolves a color name in the ``context`` color map. .. deprecated:: 4.10 This api will be removed in GTK 5 :param color_name: color name to lookup .. method:: remove_class(class_name: str) -> None Removes ``class_name`` from ``context``. .. deprecated:: 4.10 Use :obj:`~gi.repository.Gtk.Widget.remove_css_class` instead :param class_name: class name to remove .. method:: remove_provider(provider: ~gi.repository.Gtk.StyleProvider) -> None Removes ``provider`` from the style providers list in ``context``. .. deprecated:: 4.10 Please do not use it in newly written code :param provider: a ``GtkStyleProvider`` .. method:: remove_provider_for_display(display: ~gi.repository.Gdk.Display, provider: ~gi.repository.Gtk.StyleProvider) -> None Removes ``provider`` from the global style providers list in ``display``. :param display: a ``GdkDisplay`` :param provider: a ``GtkStyleProvider`` .. method:: restore() -> None Restores ``context`` state to a previous stage. See :obj:`~gi.repository.Gtk.StyleContext.save`. .. deprecated:: 4.10 This API will be removed in GTK 5 .. method:: save() -> None Saves the ``context`` state. This allows temporary modifications done through :obj:`~gi.repository.Gtk.StyleContext.add_class`, :obj:`~gi.repository.Gtk.StyleContext.remove_class`, :obj:`~gi.repository.Gtk.StyleContext.set_state` to be quickly reverted in one go through :obj:`~gi.repository.Gtk.StyleContext.restore`. The matching call to :obj:`~gi.repository.Gtk.StyleContext.restore` must be done before GTK returns to the main loop. .. deprecated:: 4.10 This API will be removed in GTK 5 .. method:: set_display(display: ~gi.repository.Gdk.Display) -> None Attaches ``context`` 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 :obj:`~gi.repository.Gtk.Widget.get_style_context`, you do not need to call this yourself. .. deprecated:: 4.10 You should not use this api :param display: a ``GdkDisplay`` .. method:: set_scale(scale: int) -> None Sets the scale to use when getting image assets for the style. .. deprecated:: 4.10 You should not use this api :param scale: scale .. method:: set_state(flags: ~gi.repository.Gtk.StateFlags) -> None Sets the state to be used for style matching. .. deprecated:: 4.10 You should not use this api :param flags: state to represent .. method:: to_string(flags: ~gi.repository.Gtk.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:: 4.10 This api will be removed in GTK 5 :param flags: Flags that determine what to print Properties ---------- .. rst-class:: interim-class .. class:: StyleContext :no-index: .. attribute:: props.display :type: ~gi.repository.Gdk.Display Virtual Methods --------------- .. rst-class:: interim-class .. class:: StyleContext :no-index: .. method:: do_changed() -> None Fields ------ .. rst-class:: interim-class .. class:: StyleContext :no-index: .. attribute:: parent_object