With introduction of GObject introspection the way to access theme colors through widget.get_style()
method is gone. I am interested on how to get theme colors when GTK+ is used through GOBject introspection. The solution should preferably work with both versions (2 and 3) but a solution for each of these is acceptable as well.
I'm not sure how to get it from gtk+-2.0, unless your using a pure gtk+-2.0 environment, in which case I think the old GtkStyle methods work. for example, assuming your not running a Gtk-3.0 environment like gnome-shell
import gi
# make sure you use gtk+-2.0
gi.require_version('Gtk', '2.0')
from gi.repository import Gtk
window = Gtk.Window()
...
style = window.get_style()
print style.lookup_color('fg_color')
I think that should still work under a gtk+-2.0 environment. I don't know for sure as my system is running gnome-shell, and can't easily try this out.
However this method has been deprecated and replaced by GtkStyleContext. If I use the above code in a gtk+-3.0 environment like gnome-shell it will run, but does not give me the information I'm after. What I get is
(False, <Gdk.Color(red=0, green=0, blue=0)>)
If I want colour information I want to use the new GtkStyleContext, for example
import gi
# make sure you use gtk+-3.0
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
window = Gtk.Window()
...
style_context = window.get_style_context()
print style_context.lookup_color('fg_color')
this will give me some real data, which is telling me the 'fg_color' has been found, due to the first entry in the tuple is True.
(True, <Gdk.Color(red=0.000000, green=0.000000, blue=0.000000, alpha=1.000000)>)
I hope this answers your question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With