Search code examples
gtk3pygtkgtktreeview

Python: Changing the select colour in a GtkTreeview


I want to disable the selection colour at a treeview. So I want to set the selected color to white with modify_base. I found this solution, but it doesn't work. This is my code:

import gi
from gi.repository import Gdk, Gtk
gi.require_version('Gtk', '3.0')

treestore = InterfaceTreeStore()
treeview = Gtk.TreeView()
treeview.set_model(treestore)

treeview.modify_base(Gtk.StateFlags.SELECTED, Gdk.Color(red=65535, blue=65535, green=65535))

Solution

  • gtk_widget_modify_base has been deprecated since 3.0. You could have used gtk_widget_override_background_color, if it wasn't deprecated since 3.16. It's documentation states that:

    If you wish to change the way a widget renders its background you should use a custom CSS style

    However, if you want to disable selection color, the simpliest way is to unselect.

    Your "changed" signal callback might look something like this:

    def changed_cb(selection):
        model, iter = get_selected (selection)
        # if there is no selection, iter is None
        if iter is None:
            return
        # do something useful
        # now unselect
        path = model.get_path(iter)
        selection.unselect_path(path)
        path.free() # not sure if python frees it automatically