How do I obtain the Gtk3 selection color of a widget using GtkStyleContext
Question:
I’m trying to obtain the selection color of a Gtk3 IconView in python but I’m confused how to interpret the existing C++ documentation and how it relates to Python.
My current python code is as follows:
color = self.iconview.get_style().bg[Gtk.StateType.SELECTED]
This works ok in Ubuntu 12.04 – Gnome/Gtk 3.2 I think.
However the documentation here says get_style
is deprecated since 3.0
In Ubuntu 12.10 which uses the latest GTK, the above does not work – I get an error:
CRITICAL **: StackOverflow protection. Can't copy array element into GIArgument
The document says I should use GtkStyleContext – but how?
Can anyone give me a concrete python example?
Answers:
In C:
GdkRGBA color;
GtkStyleContext *style =
gtk_widget_get_style_context(iconview);
gtk_style_context_get_background_color
(style, GTK_STATE_FLAG_SELECTED, &color);
Python translation by fossfreedom:
context = self.iconview.get_style_context()
color = context.get_background_color(Gtk.StateFlags.SELECTED)
It appears that the GtkStyle
struct from gtk2 was simply replaced with the more modern GtkStyleContext
class in gtk3
The new answer is “you don’t”. There isn’t necessarily a single background color any more.
Per the documentation of https://developer.gnome.org/gtk3/stable/GtkStyleContext.html#gtk-style-context-get-background-color:
“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.”
I guess this would somehow also work for the IconView. You can use Gtk.StyleContext().lookup_color(name)
(https://lazka.github.io/pgi-docs/#Gtk-3.0/classes/StyleContext.html#Gtk.StyleContext.lookup_color), which works for various colors of the theme
Here is an example
- code with fallback mechanism: https://github.com/sezanzeb/input-remapper/blob/024498109e3817738f3076ed73bd0cd94ad9bf05/inputremapper/gui/components/editor.py#L945
- those names can also be used in css files: https://github.com/sezanzeb/input-remapper/blob/e22d13832376e3a2dd6b753087e436b3ce94d80b/data/style.css#L28
- pictures: https://github.com/sezanzeb/input-remapper/pull/484#issuecomment-1279695258
The names you see in the code are extracted from the /usr/share/themes
folder, searching for @define-color
statements
To my knowledge this doesn’t use any deprecated function.
I’m trying to obtain the selection color of a Gtk3 IconView in python but I’m confused how to interpret the existing C++ documentation and how it relates to Python.
My current python code is as follows:
color = self.iconview.get_style().bg[Gtk.StateType.SELECTED]
This works ok in Ubuntu 12.04 – Gnome/Gtk 3.2 I think.
However the documentation here says get_style
is deprecated since 3.0
In Ubuntu 12.10 which uses the latest GTK, the above does not work – I get an error:
CRITICAL **: StackOverflow protection. Can't copy array element into GIArgument
The document says I should use GtkStyleContext – but how?
Can anyone give me a concrete python example?
In C:
GdkRGBA color;
GtkStyleContext *style =
gtk_widget_get_style_context(iconview);
gtk_style_context_get_background_color
(style, GTK_STATE_FLAG_SELECTED, &color);
Python translation by fossfreedom:
context = self.iconview.get_style_context()
color = context.get_background_color(Gtk.StateFlags.SELECTED)
It appears that the GtkStyle
struct from gtk2 was simply replaced with the more modern GtkStyleContext
class in gtk3
The new answer is “you don’t”. There isn’t necessarily a single background color any more.
Per the documentation of https://developer.gnome.org/gtk3/stable/GtkStyleContext.html#gtk-style-context-get-background-color:
“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.”
I guess this would somehow also work for the IconView. You can use Gtk.StyleContext().lookup_color(name)
(https://lazka.github.io/pgi-docs/#Gtk-3.0/classes/StyleContext.html#Gtk.StyleContext.lookup_color), which works for various colors of the theme
Here is an example
- code with fallback mechanism: https://github.com/sezanzeb/input-remapper/blob/024498109e3817738f3076ed73bd0cd94ad9bf05/inputremapper/gui/components/editor.py#L945
- those names can also be used in css files: https://github.com/sezanzeb/input-remapper/blob/e22d13832376e3a2dd6b753087e436b3ce94d80b/data/style.css#L28
- pictures: https://github.com/sezanzeb/input-remapper/pull/484#issuecomment-1279695258
The names you see in the code are extracted from the /usr/share/themes
folder, searching for @define-color
statements
To my knowledge this doesn’t use any deprecated function.