Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Ubuntu used to have alternating row colors for gtk TreeView widgets. Some applications still do, like Banshee. Alternating row colors would really help my application (written in python 2.7, and using gi.repository and not pygtk) that I'm developing.

I know I can set a hint with

treeview.set_rules_hint(True)

This isn't working, however. Does anyone know how to enable the alternating row colors?

share|improve this question

1 Answer 1

Whether or not set_rules_hint(True) will display alternating row colors is up to the theme. With Ubuntu's default, Ambience, there will not be alternating rows. As the documentation states:

Do not use it just because you prefer the appearance of the ruled tree; that's a question for the theme. Some themes will draw tree rows in alternating colors even when rules are turned off, and users who prefer that appearance all the time can choose those themes. You should call this function only as a semantic hint to the theme engine that your tree makes alternating colors useful from a functional standpoint (since it has lots of columns, generally).

That said, you can force it if your really want. For instance, this will work in Ambience:

view.set_rules_hint(True)
style_context = self.get_style_context()
css_provider = Gtk.CssProvider()
css = """
    GtkTreeView row:nth-child(even) {
        background-color: shade(@base_color, 0.9);
    }

    GtkTreeView row:nth-child(odd) {
        background-color: shade(@base_color, 1.0);
    }
    """
css_provider.load_from_data(css.encode('UTF-8'))
screen = Gdk.Screen.get_default()
style_context.add_provider_for_screen(screen, css_provider,
                                  Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

Though you need to be aware that it might break in other themes. Some themes will use @theme_base_color instead of @base_color You can of course hard code the color, but who knows how that will look with the user's theme...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.