How to get widget background color in GTK and Python?

I want to get the normal background color of the widget (a GtkHeaderBar, in this case). I am currently using

style = self.get_titlebar().get_style_context()

to get style, and

color = style.get_property("background-color", Gtk.StateFlags.NORMAL)

to get the background color associated with this style.

However, it returns an object Gkd.RGBAwith the following properties:

Gdk.RGBA(red=0.000000, green=0.000000, blue=0.000000, alpha=0.000000)

But if I open the GTK Inspector, select the HeaderBar and move on to the style properties, it shows

background-color | rgb(57,63,63) | gtk-contained-dark.css:1568.29

What do I need to do to get the same values?

Edit:

I am experimenting with GtkStyleContext.render_background(), but I have no success:

surfc = Cairo.ImageSurface (Cairo.FORMAT_ARGB32, 10, 10)
contx = Cairo.Context(surfc)
style = self.get_titlebar().get_style_context()
backg = Gtk.render_background(style, contx, 10, 10, 10, 10)
surfc.write_to_png("test.png")

The resulting file test.pngis an image rgba(0, 0, 0, 0).

+4
source share
2 answers

css. . python

css_provider = Gtk.CssProvider()
css_provider.load_from_path('application.css')
Gtk.StyleContext.add_provider_for_screen(
    Gdk.Screen.get_default(),
    css_provider,
    Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
)

css, :

GtkHeaderbar {
    background-color:@theme_bg_color;
}

EDIT:. , , . widget.get_style_context().get_background_color(), - Gdk.RGBA(red=0.913725, green=0.913725, blue=0.913725, alpha=1.000000).

, get_background_color() , . , . . .

+2

- , , :

backg = Gtk.render_background(style, contx, 10, 10, 10, 10)

:

backg = Gtk.render_background(style, contx, 0, 0, 10, 10)
0

Source: https://habr.com/ru/post/1599655/


All Articles