你如何改变pygtk中gtk.TreeView的交替背景行颜色? [英] How do you change alternating background row colors of a gtk.TreeView in pygtk?

查看:283
本文介绍了你如何改变pygtk中gtk.TreeView的交替背景行颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改变treeview的交替背景颜色。我知道这通常应该留给主题,但我想重写以测试gtk样式功能。根据treeview文档这里,我了解到TreeView有几种风格选项是只读的,包括偶数行颜色,奇数行颜色和允许规则(根据文档,允许绘制偶数和奇数行颜色)。我知道为了覆盖这些只读设置,我必须改变gtkrc样式文件或字符串中的样式。



所以我的字符串treeview看起来像:

  gtk.rc_parse_string(
stylecustom-treestyle{
GtkTreeView :: odd-row-color =#00CBFF
GtkTreeView :: even-row-color =#90EE90
GtkTreeView :: allow-rules = 1
}
widget* custom_treeview *stylecustom-treestyle

treeview.set_name(custom_treeview)

这个解析没有错误,结果是偶数行颜色被应用于偶数行和奇数行。



编辑:我从一些更多的测试中发现,我的解析字符串必须被其他一些样式设置覆盖。



< pre打印treeview.style_get_property('allow-rules')
打印treeview.style_get_property('odd-row-color')
打印treeview.style_get_property('even )

给出结果:

  True 
None
None

这些都是默认设置。通常我会认为它不会解析字符串并设置适当的值,但在这种情况下,背景颜色确实改变为我指定的颜色(仅将每行背景涂成一种颜色)。



我做错了什么?

解决方案

我终于弄清楚了我的问题与这种造型方法。
'allow-rules'样式属性不是唯一的属性,它告诉树视图以交替颜色对行进行着色。根据文档,treeview具有另一个属性,它向引擎提示它应该以交替颜色绘制行。默认情况下,'True-Hint'属性为False,当True成功地为交替的奇数/偶数行颜色着色我的TreeView!

所以,在代码中,将此行添加到问题中的代码:

  treeview.set_rules_hint(True)

就是这样,一个带有交替彩色行的TreeView!


I'm trying to change the alternating background color of a treeview. I know that this should normally be left up to the theme, but I'd like to override to test the gtk Style functionality. According to the treeview documentation here, I learned that the TreeView has several style options that are Read-only, including "even-row-color", "odd-row-color" and "allow-rules"(which according to the documentation, allows drawing of even and odd row colors). And I know that in order to override those Read-only settings I've got to change the style in a gtkrc-style file or string.

So my string for a treeview looks like:

    gtk.rc_parse_string( """
        style "custom-treestyle"{
            GtkTreeView::odd-row-color = "#00CBFF"
            GtkTreeView::even-row-color = "#90EE90"
            GtkTreeView::allow-rules = 1
        }
        widget "*custom_treeview*" style "custom-treestyle"
    """)
    treeview.set_name("custom_treeview" )

This parses without error and the result is that the even-row-color gets applied to both even and odd rows.

EDIT: I discovered from some more testing that my parse string must be getting overridden from some other style settings.

print treeview.style_get_property( 'allow-rules' )
print treeview.style_get_property( 'odd-row-color')
print treeview.style_get_property( 'even-row-color')

Gives the result:

True
None
None

Which are all default settings. Normally I would think that it's simply not parsing the string and setting the appropriate values, but in this case the background color does change to the color I specified( only it paints every row's background to one color).

What am I doing wrong?

解决方案

I finally figured out my issue with this styling method. The 'allow-rules' style property isn't the only property that tells the treeview to color the rows in alternating colors. According to the documentation, the treeview has another property that hints to the engine that it should draw rows in alternating colors. The 'rules-hint' property, False by default, when True successfully colors my TreeView in alternating odd/even row colors!

So, in code, add this line to the code in the question:

treeview.set_rules_hint( True )

And that's it, a TreeView with alternating colored rows!

这篇关于你如何改变pygtk中gtk.TreeView的交替背景行颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆