Search code examples
pythonfilteringpygtkgtktreeview

PyGtk How to change TreeView data using filter?


What I'm trying to do is dynamically change (via filter) list of strings. In my case it is list of files with paths stored in gtk.TreeStore.

Example of expected effect (eg. Sublime Text 2 file searching Ctrl+P):

Data:

List of files (treeview):

  • '/home/user/'
    • '.bashrc',
    • 'test.txt',
  • ...

Action: In filter field (Entry widget) user starts typing. After first character eg. 't' list should be updated like this:

Result:

List of files (treeview):

  • 'test.txt',
  • ... all strings containing 't' (matching function is not important now)

What I already have:

self.search_entry = gtk.Entry()

self.tree_store = gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_BOOLEAN)

for node, files in self.files_list:
    parent = self.tree_store.append(None, [node, True])
    for f in files:
        self.tree_store.append(parent, [f, True])

tree_filter = self.tree_store.filter_new()
self.treeview = gtk.TreeView(tree_filter)

self.renderer0 = gtk.CellRendererText()
self.treeview.insert_column_with_attributes(-1, "Filename", self.renderer0, text=0)
self.treeview.set_search_column(0)
self.treeview.set_search_entry(self.search_entry)
self.treeview.expand_all()

I know that TreeView has a method set_search_equal_func where I can change default matching function to my own. The problem is how can I change the list during typing in filter form field?


Solution

  • You must read about gtk.TreeModelFilter. Here you can read a very useful example. It's C#, but it's very easy to "translate" it to python:

    http://www.mono-project.com/GtkSharp_TreeView_Tutorial#Filtering_Data