Python Forum

Full Version: How can I get Colors of current GTK theme?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
On ArchLinux, I'm trying to get the Colors of the current GTK3 theme, but without opening a gtk window.
All I want the script to do is get the colors (and possibly other style attributes), choose a few, and print them to a file. no gui stuff.
I'm confident I can handle the selection & printing to file, but how to get at the colors in the first place?

I need this for gtk3 (and i assume that i also have to use python3 for this).

I have a working draft for gtk2:
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-

import gtk

sep="============================================"

w = gtk.Window()
w.realize()
style=w.get_style()

print '''A style contains the graphics information needed by a widget to draw itself in its various states:

 STATE_NORMAL        # The state during normal operation.
 STATE_ACTIVE        # The widget is currently active, such as a button pushed
 STATE_PRELIGHT      # The mouse pointer is over the widget.
 STATE_SELECTED      # The widget is selected
 STATE_INSENSITIVE   # The widget is disabled

A style contains the following attributes:
'''

l=[gtk.STATE_NORMAL,gtk.STATE_ACTIVE,gtk.STATE_PRELIGHT,gtk.STATE_SELECTED,gtk.STATE_INSENSITIVE]

print sep,"\nbase - a list of 5 colors"
for i in l:
print "\t",style.base[i].to_string(),i

print sep,"\ntext - a list of 5 colors"
for i in l:
print "\t",style.text[i].to_string(),i

print sep,"\nfg - a list of 5 foreground colors - one for each state"
for i in l:
print "\t",style.fg[i].to_string(),i

print sep,"\nbg - a list of 5 background colors"
for i in l:
print "\t",style.bg[i].to_string(),i

print sep,"\nlight - a list of 5 colors - created during set_style() method"
for i in l:
print "\t",style.light[i].to_string(),i

print sep,"\ndark - a list of 5 colors - created during set_style() method"
for i in l:
print "\t",style.dark[i].to_string(),i

print sep,"\nmid - a list of 5 colors - created during set_style() method"
for i in l:
print "\t",style.mid[i].to_string(),i

print sep,"\ntext_aa - a list of 5 colors halfway between text/base"
for i in l:
print "\t",style.text_aa[i].to_string(),i
and here's what i came up with for gtk3 (after hours of trawling the web and trying out things):
#!/usr/bin/env python
# -*- coding: utf-8 -*-

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

win = Gtk.Window()
#~ win.connect("delete-event", Gtk.main_quit)
#~ win.show_all()
#~ Gtk.main()
style_context = win.get_style_context()
print(style_context)
print(style_context.lookup_color('bg_color')) # if this worked i'd still need to get a list of valid attributes
and it gives me this:
Output:
<Gtk.StyleContext object at 0x7f82975d0090 (GtkStyleContext at 0x130f320)> (False, color=Gdk.RGBA(red=0.000000, green=0.000000, blue=0.000000, alpha=0.000000))
the first one is too low-level for me (ram addresses?!), the second clearly shows that i'm not getting what i'm looking for.

any help appreciated, thanks.

o.
I'm not very familiar with gtk, but found this (using pygtk and package): http://python.6.x6.nabble.com/GTK3-get-b...03859.html

looks like there's something you can use here to extract RGB values
thanks!
(May-25-2017, 05:26 PM)Larz60+ Wrote: [ -> ](using pygtk and package)
afaiu pygtk is python2 and works only with gtk2.
if you look at post #1, that's what i used to implement the gtk2 solution.

for gtk3, this:
Quote:http://python.6.x6.nabble.com/GTK3-get-b...03859.html
is a little closer to what I want.

i can now get some information about gtk colors without showing a window:
#!/usr/bin/env python

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

def on_button_clicked(button, tv):
    style = tv.get_style_context()
    bgcolor = style.get_background_color(Gtk.StateType.NORMAL) 
    print(bgcolor) 

win = Gtk.Window()
vb = Gtk.VBox()
tv = Gtk.TextView()
vb.add(tv)
win.show_all()
style = tv.get_style_context()
bgcolor = style.get_background_color(Gtk.StateType.NORMAL) 
print(bgcolor)
#settings = Gtk.Settings.get_default()
#for i in settings.list_properties():
#    print(i)
this will give me reasonable color values:
Output:
Gdk.RGBA(red=0.866667, green=0.866667, blue=0.866667, alpha=1.000000)
halfway there.

if i uncomment the last 3 lines i also get a long list of gtk3 properties; but i
- don't know how to get at the values
- don't know if they're even filled with current values.

any help appreciated.

o.