Get colors of current gtk style

I use PyGTK and I want to get colors of a widget (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)


我偶然发现了同样的问题,看到了你的问题,并找到了一个解决方案:你必须等到widget实现,例如这样:

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)
链接地址: http://www.djcxy.com/p/37898.html

上一篇: matplotlib和wxpython的透明度问题

下一篇: 获取当前gtk风格的颜色