问题
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?
回答1:
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!
来源:https://stackoverflow.com/questions/9036812/how-do-you-change-alternating-background-row-colors-of-a-gtk-treeview-in-pygtk