[GTK3] get background color

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

[GTK3] get background color

Yann Leboulanger
Hi,

I'm trying to get the default / current background color of a textview.
The code I do it:

context = tv.get_style_context()
color = context.get_background_color(Gtk.StateFlags.NORMAL)

But that returns a fully transparent color (0,0,0,0)

If I first set a custom color with tv.override_background_color(), then
get_background_color() correctly works and returns the color I set.

So how can I get the current default color?

Thanks in advanced
--
Yann
_______________________________________________
pygtk mailing list   [hidden email]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/
Reply | Threaded
Open this post in threaded view
|

Re: [GTK3] get background color

Niklas Koep
Sounds like you're trying to retrieve the default colors before the widget has been realized. Consider this (mind you this uses pygtk):

#!/usr/bin/env python

import gtk


def main():
    window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    window.connect("delete-event", lambda *x: gtk.main_quit())

    tv = gtk.TreeView()
    tv.set_size_request(400, 250)

    def realize(tv): print tv.get_style().bg[gtk.STATE_ACTIVE]
    tv.connect("realize", realize)
    realize(tv)

    window.add(tv)
    window.show_all()

    gtk.main()

if __name__ == "__main__":
    main()


The output of this example on my machine is

#dcdad5
#f0f0f0

which just goes to show that the colors before and after realization (might) differ.

Regards, Niklas



2013/1/29 Yann Leboulanger <[hidden email]>
Hi,

I'm trying to get the default / current background color of a textview. The code I do it:

context = tv.get_style_context()
color = context.get_background_color(Gtk.StateFlags.NORMAL)

But that returns a fully transparent color (0,0,0,0)

If I first set a custom color with tv.override_background_color(), then get_background_color() correctly works and returns the color I set.

So how can I get the current default color?

Thanks in advanced
--
Yann
_______________________________________________
pygtk mailing list   [hidden email]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/


_______________________________________________
pygtk mailing list   [hidden email]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/
Reply | Threaded
Open this post in threaded view
|

Re: [GTK3] get background color

Yann Leboulanger
On 01/29/2013 02:35 PM, Niklas Koep wrote:
> Sounds like you're trying to retrieve the default colors before the
> widget has been realized. Consider this (mind you this uses pygtk):

No, this is done AFTER the widget is realized. It's done in a textbiffer
'changed' callback, a long time (several seconds) after widget is shown.

Your code is what I used in pygtk, but I'm not able to find a working
equivalent in pygobject.

--
Yann
_______________________________________________
pygtk mailing list   [hidden email]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/
Reply | Threaded
Open this post in threaded view
|

Re: [GTK3] get background color

Yann Leboulanger
In reply to this post by Yann Leboulanger
On 01/29/2013 02:18 PM, Yann Leboulanger wrote:

> Hi,
>
> I'm trying to get the default / current background color of a textview.
> The code I do it:
>
> context = tv.get_style_context()
> color = context.get_background_color(Gtk.StateFlags.NORMAL)
>
> But that returns a fully transparent color (0,0,0,0)
>
> If I first set a custom color with tv.override_background_color(), then
> get_background_color() correctly works and returns the color I set.
>
> So how can I get the current default color?
>

I still can't get it working with GTK-3.8.2 and python3-gi 3.8.3

Any idea?
--
Yann
_______________________________________________
pygtk mailing list   [hidden email]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/
Reply | Threaded
Open this post in threaded view
|

Re: [GTK3] get background color

Timo List
Op 31-07-13 13:48, Yann Leboulanger schreef:

> On 01/29/2013 02:18 PM, Yann Leboulanger wrote:
>> Hi,
>>
>> I'm trying to get the default / current background color of a textview.
>> The code I do it:
>>
>> context = tv.get_style_context()
>> color = context.get_background_color(Gtk.StateFlags.NORMAL)
>>
>> But that returns a fully transparent color (0,0,0,0)
>>
>> If I first set a custom color with tv.override_background_color(), then
>> get_background_color() correctly works and returns the color I set.
>>
>> So how can I get the current default color?
>>
>
> I still can't get it working with GTK-3.8.2 and python3-gi 3.8.3
>
> Any idea?

Did you wait untill the treeview is realized?

treeview.connect('realize', get_bg_color)

def get_bg_color(widget):
     style = widget.get_style_context()
     bgcolor = style.get_background_color(Gtk.StateType.NORMAL)
     print(bgcolor)


Cheers,
Timo
_______________________________________________
pygtk mailing list   [hidden email]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/
Reply | Threaded
Open this post in threaded view
|

Re: [GTK3] get background color

Yann Leboulanger
On 07/31/2013 07:33 PM, Timo wrote:

> Op 31-07-13 13:48, Yann Leboulanger schreef:
>> On 01/29/2013 02:18 PM, Yann Leboulanger wrote:
>>> Hi,
>>>
>>> I'm trying to get the default / current background color of a textview.
>>> The code I do it:
>>>
>>> context = tv.get_style_context()
>>> color = context.get_background_color(Gtk.StateFlags.NORMAL)
>>>
>>> But that returns a fully transparent color (0,0,0,0)
>>>
>>> If I first set a custom color with tv.override_background_color(), then
>>> get_background_color() correctly works and returns the color I set.
>>>
>>> So how can I get the current default color?
>>>
>>
>> I still can't get it working with GTK-3.8.2 and python3-gi 3.8.3
>>
>> Any idea?
>
> Did you wait untill the treeview is realized?
>
> treeview.connect('realize', get_bg_color)
>
> def get_bg_color(widget):
>      style = widget.get_style_context()
>      bgcolor = style.get_background_color(Gtk.StateType.NORMAL)
>      print(bgcolor)

Yes treeview is realized for sure, this code happens in a callback when
I press a key.

--
Yann

_______________________________________________
pygtk mailing list   [hidden email]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/
Reply | Threaded
Open this post in threaded view
|

Re: [GTK3] get background color

Yann Leboulanger
On 07/31/2013 09:03 PM, Yann Leboulanger wrote:

> On 07/31/2013 07:33 PM, Timo wrote:
>> Op 31-07-13 13:48, Yann Leboulanger schreef:
>>> On 01/29/2013 02:18 PM, Yann Leboulanger wrote:
>>>> Hi,
>>>>
>>>> I'm trying to get the default / current background color of a textview.
>>>> The code I do it:
>>>>
>>>> context = tv.get_style_context()
>>>> color = context.get_background_color(Gtk.StateFlags.NORMAL)
>>>>
>>>> But that returns a fully transparent color (0,0,0,0)
>>>>
>>>> If I first set a custom color with tv.override_background_color(), then
>>>> get_background_color() correctly works and returns the color I set.
>>>>
>>>> So how can I get the current default color?
>>>>
>>>
>>> I still can't get it working with GTK-3.8.2 and python3-gi 3.8.3
>>>
>>> Any idea?
>>
>> Did you wait untill the treeview is realized?
>>
>> treeview.connect('realize', get_bg_color)
>>
>> def get_bg_color(widget):
>>      style = widget.get_style_context()
>>      bgcolor = style.get_background_color(Gtk.StateType.NORMAL)
>>      print(bgcolor)
>
> Yes treeview is realized for sure, this code happens in a callback when
> I press a key.
>
Here is a very simple scrpit that shows the problem. For me it prints:
<Gdk.Color(red=0.000000, green=0.000000, blue=0.000000, alpha=0.000000)>

--
Yann

_______________________________________________
pygtk mailing list   [hidden email]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/

test_tv3.py (451 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [GTK3] get background color

Osmo Salomaa-2
> Here is a very simple scrpit that shows the problem. For me it prints:
> <Gdk.Color(red=0.000000, green=0.000000, blue=0.000000, alpha=0.000000)>

For editable text fields, you're probably looking for the *base* color.

    style.lookup_color("theme_base_color")

For Adwaita, you can find the available color names in gtk-main.css [1],
but I really don't know if they work across different themes.

[1]
https://git.gnome.org/browse/gnome-themes-standard/tree/themes/Adwaita/gtk-3.0/gtk-main.css

--
Osmo Salomaa <[hidden email]>
_______________________________________________
pygtk mailing list   [hidden email]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/
Reply | Threaded
Open this post in threaded view
|

Re: [GTK3] get background color

Yann Leboulanger
On 08/01/2013 02:00 AM, Osmo Salomaa wrote:

>> Here is a very simple scrpit that shows the problem. For me it prints:
>> <Gdk.Color(red=0.000000, green=0.000000, blue=0.000000, alpha=0.000000)>
>
> For editable text fields, you're probably looking for the *base* color.
>
>      style.lookup_color("theme_base_color")
>
> For Adwaita, you can find the available color names in gtk-main.css [1],
> but I really don't know if they work across different themes.
>
> [1]
> https://git.gnome.org/browse/gnome-themes-standard/tree/themes/Adwaita/gtk-3.0/gtk-main.css
>

I am under XFCE / GTK2 theme, so there is no such file in my theme. My
gtkrc file has a base[NORMAL] = "#ffffff" line, but I don't know how to
get that with gtk3. It should be possible as it's correctly rendered.

style.lookup_color("theme_base_color") returns a 0, 0, 0, 0 color here.

--
Yann
_______________________________________________
pygtk mailing list   [hidden email]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/
Reply | Threaded
Open this post in threaded view
|

Re: [GTK3] get background color

Osmo Salomaa-2
01.08.2013 10:09, Yann Leboulanger wrote:
> On 08/01/2013 02:00 AM, Osmo Salomaa wrote:
> style.lookup_color("theme_base_color") returns a 0, 0, 0, 0 color here.

OK, so that's theme-specific.

Some color names might be available regardless of theme, but I don't
know which ones; for example Gtk.InfoBar documentation explicitly
mentions {info,warning,question,other}_{bg,fg}_color.

I get the same (0,0,0,0) from get_background_color with your test
script. I've ran into this issue before and I have just used those
Adwaita names and a not-well-thought-out fallback if the lookup fails.

Curiously, if I swap the text view in your script for an entry, I get
the actual background color (white). In any reasonable theme, these
should probably both use the "base" color.

--
Osmo Salomaa <[hidden email]>
_______________________________________________
pygtk mailing list   [hidden email]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/