开发者

Get colors of current gtk style

开发者 https://www.devze.com 2023-03-20 13:20 出处:网络
I use PyGTK and I want to get colors of a widge开发者_开发问答t (for example bg color), I run such a code:

I use PyGTK and I want to get colors of a widge开发者_开发问答t (for example bg color), I run such a code:

gdkColorToRgb = lambda gc: (gc.red//257, gc.green//257, gc.blue//257)
widget = gtk.HBox() ## for example
style = widget.get_style()
for i in range(5):
    print i, gdkColorToRgb(style.bg[i])

But it does not give colors of my current gtk theme(style). It seems to be for default gtk theme (my current theme is dark, while this code gives light colors) I use ArchLinux and PyGTK 2.24.0 (GTK 2.24.5)


I just stumbled upon the same issue, saw your question and found a solution: You have to wait until the widget is realized, e.g. like this:

def print_style(widget):
    style = widget.get_style()
    for i in range(5):
        print i, gdkColorToRgb(style.bg[i])
gdkColorToRgb = lambda gc: (gc.red//257, gc.green//257, gc.blue//257)
widget = gtk.HBox() ## for example
widget.connect('realize', print_style)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号