Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the default colors in GTK?

Context

In GTK 3, people can set their own themes. Even the default theme (Adwaita) is provided with two variants: a light one and a dark one. As I am writing my own widget (in python), I need to get these colors in order to avoid drawing black on black or white on white.

Question

How can I access the default colors of the user GTK theme?


Things that don't work

  • GtkSettings used to provide an acceptable gtk-theme-color property, but it is not there anymore and there is no reference in the doc to explain how to replace that.
  • I don't want to get that color from another widget:
    • This is of no use: PyGTK does not work with Python/GTK 3 and that would require another widget.
    • This would be better (PyGObject-based at least) but again, thar would require that I copy the style from another widget.

Why don't I want to copy the style from another widget?

Because the pristine color is there somewhere. I don't see, in principle, why I should be forced to access it indirectly.

Moreover, how would you react if you crashed a program just because you dared removing a single label somewhere? How would you react if changing the color of a single label actually changed the color of other, completely unrelated, widgets?

I don't want this kind of surprises.

like image 721
JohnW Avatar asked Aug 10 '16 10:08

JohnW


1 Answers

You don't need to instantiate GTK widgets to retrieve their StyleContext.

You can create an empty Gtk.StyleContext and set the Gtk.WidgetPath of a widget class.

The foreground color can be retrieved with .get_color(). Other colors and style properties can be retrieved with .get_property().

Both methods need Gtk.StateFlags.

For properties, see GTK+ CSS Overview and GTK+ CSS Properties.

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

# Create an empty style context
style_ctx = Gtk.StyleContext();

# Create an empty widget path
widget_path =  Gtk.WidgetPath();

# Specify the widget class type you want to get colors from
widget_path.append_type(Gtk.Button);
style_ctx.set_path(widget_path);

# Print style context colors of widget class Gtk.Button
print('Gtk.Button: Normal:')
print('foreground color: ', style_ctx.get_color(Gtk.StateFlags.NORMAL) )
print('color:            ', style_ctx.get_property('color', Gtk.StateFlags.NORMAL) )
print('background color: ', style_ctx.get_property('background-color', Gtk.StateFlags.NORMAL) )
print('outline color:    ', style_ctx.get_property('outline-color', Gtk.StateFlags.NORMAL) )

print('Gtk.Button: Link:')
print('foreground color: ', style_ctx.get_color(Gtk.StateFlags.LINK) )
print('color:            ', style_ctx.get_property('color', Gtk.StateFlags.LINK) )
print('background color: ', style_ctx.get_property('background-color', Gtk.StateFlags.LINK) )
print('outline color:    ', style_ctx.get_property('outline-color', Gtk.StateFlags.LINK) )
like image 105
Peter Holzer Avatar answered Oct 11 '22 09:10

Peter Holzer