#!/usr/bin/env python # example basictreeview.py import pygtk pygtk.require('2.0') import gtk, gobject, sys, os, stat, time class BasicTreeViewExample: # close the window and quit def delete_event(self, widget, event, data=None): gtk.main_quit() return False def __init__(self): # Create a new window self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) self.window.set_title("Basic TreeView Example") self.window.set_size_request(200, 600) self.window.connect("delete_event", self.delete_event) path = os.path.expanduser('~') self.basepath = path self.treestore = gtk.TreeStore( gobject.TYPE_STRING, gobject.TYPE_STRING ) self.matchPathTreetoModelTree(path, self.treestore) # create the TreeView using treestore self.treeview = gtk.TreeView(self.treestore) # create the TreeViewColumn to display the data self.tvcolumn = gtk.TreeViewColumn('Column 0') # add tvcolumn to treeview self.treeview.append_column(self.tvcolumn) # create a CellRendererText to render the data self.cell = gtk.CellRendererText() # add the cell to the tvcolumn and allow it to expand self.tvcolumn.pack_start(self.cell, True) # set the cell "text" attribute to column 0 - retrieve text # from that column in treestore self.tvcolumn.add_attribute(self.cell, 'text', 0) # make it searchable self.treeview.set_search_column(0) # Allow sorting on the column self.tvcolumn.set_sort_column_id(0) # Allow drag and drop reordering of rows self.treeview.set_reorderable(True) self.window.add(self.treeview) self.window.show_all() def matchPathTreetoModelTree(self, path, treeStoreNode): isdir = os.path.isdir join = os.path.join append = treeStoreNode.append recurse = self.matchPathTreetoModelTree filenames = os.listdir(path) for filename in filenames: if filename.startswith('.'): continue filepath = join(path, filename) print >> sys.stderr, "%s is a %s" % (filepath, ('file', 'dir')[isdir(filepath)]) # nb: you may want to filter out non-regular files # like devices, sockects etc here newnode = append(None, (filepath, None)) if isdir(filepath): recurse(filepath, newnode) def main(): gtk.main() if __name__ == "__main__": tvexample = BasicTreeViewExample() main()