[pygtk] Sorting Treemodelfilter
Kalman KHIRALY
khiraly123 at gmx.net
Mon Feb 9 03:01:17 WST 2009
Hi!
I want to display a tree, where each row is a python object, and another
function(modify_func) retieves the
correct data from this object and adds to each column.
I have written a simple example program, which achieves exactly what I want.
There is just one more glitch:
I cant make neither of the column sortable.
So can somebody help me how can I make the second (or first) column
clickable and sortable?
The ready-to-use demo source can be found here:
http://www.pastebin.ca/1330860
I paste here too, for future reference:
#!/usr/bin/env python
# example treeviewcolumn.py
import pygtk
pygtk.require('2.0')
import gtk
class TreeViewColumnExample:
# close the window and quit
def delete_event(self, widget, event, data=None):
gtk.main_quit()
return False
def modify_func(self, model, iter, col, attrs):
"""Extract the columns of a single python object."""
# Convert the filter iter to the corresponding child model iter.
child_model_iter = model.convert_iter_to_child_iter(iter)
child_model = model.get_model()
row_obj = child_model.get_value(child_model_iter, 0)
path = child_model.get_path(child_model_iter)
path_str= "-".join(str(i) for i in path)
if col == 0: #first column
arr = self.row[path_str][col]
elif col == 1:
arr = self.row[path_str][col]
elif col == 2:
arr = self.row[path_str][col]
else:
arr = "faszomat"
return arr
def __init__(self):
# Create a new window
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title("TreeViewColumn Example")
#self.window.set_size_request(200, 200)
self.window.connect("delete_event", self.delete_event)
# create a liststore with one string column to use as the model
self.treestore = gtk.TreeStore(object)
# create the TreeView using liststore
self.treeview = gtk.TreeView()
# create the TreeViewColumns to display the data
self.tvcolumn = gtk.TreeViewColumn('Pixbuf and Text')
self.tvcolumn1 = gtk.TreeViewColumn('Text Only')
# add a row with text and a stock item - color strings for
# the background
self.row = {}
self.row["0"] = ['Open', gtk.STOCK_OPEN, 'Open a File', True]
self.row["1"] = ['New', gtk.STOCK_NEW, 'New File', True]
self.row["1-0"] = ['Save', gtk.STOCK_SAVE, 'Save File', True]
self.row["2"] = ['Print', gtk.STOCK_PRINT, 'Print File', False]
self.treestore.append(None, [self.row["0"]])
parent_iter = self.treestore.append(None, [self.row["1"]])
self.treestore.append(parent_iter, [self.row["1-0"]])
self.treestore.append(None, [self.row["2"]])
self.filtered = self.treestore.filter_new()
self.filtered.set_modify_func([str, str, str, 'gboolean'],
self.modify_func, ["data"])
self.treeview.set_model(self.filtered)
# add columns to treeview
self.treeview.append_column(self.tvcolumn)
self.treeview.append_column(self.tvcolumn1)
# create a CellRenderers to render the data
self.cellpb = gtk.CellRendererPixbuf()
self.cell = gtk.CellRendererText()
self.cell1 = gtk.CellRendererText()
# set background color property
self.cellpb.set_property('cell-background', 'yellow')
self.cell.set_property('cell-background', 'cyan')
self.cell1.set_property('cell-background', 'pink')
# add the cells to the columns - 2 in the first
self.tvcolumn.pack_start(self.cellpb, False)
self.tvcolumn.pack_start(self.cell, True)
self.tvcolumn1.pack_start(self.cell1, True)
# set the cell attributes to the appropriate liststore column
# GTK+ 2.0 doesn't support the "stock_id" property
self.tvcolumn.set_attributes(self.cellpb, stock_id=1)
self.tvcolumn.set_attributes(self.cell, text=0)
self.tvcolumn1.set_attributes(self.cell1, text=2)
# make treeview searchable
self.treeview.set_search_column(0)
# Allow sorting on the column
self.tvcolumn.set_sort_column_id(2)
# Allow drag and drop reordering of rows
#self.treeview.set_reorderable(True)
self.window.add(self.treeview)
self.window.show_all()
def main():
gtk.main()
if __name__ == "__main__":
tvcexample = TreeViewColumnExample()
main()
Best regards,
Khiraly
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.daa.com.au/pipermail/pygtk/attachments/20090208/721294d7/attachment.htm
More information about the pygtk
mailing list