11

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

style = self.get_titlebar().get_style_context()

to get the style, and

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

to get the background color associated to that style.

However it returns a Gkd.RGBA object with the following properties:

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

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

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

What do I have to do to get these same values?

Edit:

I am experimenting with the GtkStyleContext.render_background(), but I'm having 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.png is a rgba(0, 0, 0, 0) image.

TingPing
  • 2,129
  • 1
  • 12
  • 15
user3474251
  • 209
  • 2
  • 12

3 Answers3

5

You should have a look at modifying the background-color with css. There is a very good documentation of it. It can be used with python with

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
)

and a css file like:

GtkHeaderbar {
    background-color:@theme_bg_color;
}

EDIT: As you commented you do not want to modify the background color but retrieve the value of it. You can do widget.get_style_context().get_background_color() and it will return something like Gdk.RGBA(red=0.913725, green=0.913725, blue=0.913725, alpha=1.000000).

However, you should note that get_background_color() is deprecated since there is not one background color. Some widget use a gradient as a background so it is not the best solution to use this method. See the documentation for reference.

elya5
  • 2,236
  • 1
  • 11
  • 27
  • 1
    Sorry, I don't want to modify the background-color with css. I wanna get the current background-color. – user3474251 Jul 26 '15 at 23:26
  • Yeap, I'm aware of `get_background_color()`. However it always returns `rgba(0, 0, 0, 0)`. Probably because the widget is using a gradient... – user3474251 Aug 05 '15 at 19:51
  • 3
    -1, this doesn't answer the question, and `get_background_color()` is deprecated as well as not supporting backgrounds other than plain colors, which is not, if not infrequently, the case. Most buttons, bars and even window backgrounds show gradient features nowadays. – ElementW Jul 31 '16 at 16:25
  • How incredibly hard it was to stumble across this. Now I can finally paint my widgets in a different theme color. thanks. – sezanzeb Nov 03 '20 at 22:28
2

You were on the right track, but there is an issue with new recommended way to get colors.

The recommended workaround is to fall back to the deprecated get_background_color()

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

window = Gtk.Window()
context = window.get_style_context()
# this one is buggy
color1 = context.get_property("background-color", Gtk.StateFlags.NORMAL)
print(color1)
# this is the deprecated version, but it is the recommended workaround
color2 = context.get_background_color(Gtk.StateFlags.NORMAL)
print(color2)

From the python/Gtk documentation,

Note that passing a state other than the current state of self is not recommended unless the style context has been saved with Gtk.StyleContext.save()

So the current recommended way to get a widget background color is

context = widget.get_style_context()
color = context.get_background_color(widget.get_state())

Often self is a Gtk widget, and this becomes

context = self.get_style_context()
color = context.get_background_color(self.get_state())
ederag
  • 2,409
  • 25
  • 49
1

If anyone's interested, I think the problem lies here:

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

while it should be:

backg = Gtk.render_background(style, contx, 0, 0, 10, 10)
haael
  • 972
  • 2
  • 10
  • 22
  • This was not the main issue (see my answer), but this is indeed why the `render_background` resulting image had zero size. Good catch ! – ederag Feb 21 '19 at 18:12