It is not possible to create a recursive function over a pyGTK treeStore

sebastien.abeille at sebastien.abeille at
Fri Jun 29 13:30:19 CEST 2007


I would like to create a minimalist file browser using pyGTK.

Having read lot of tutorials, it seems to me that that in my case, the
best solution is
to have a gtk.TreeStore containing all the files and folders so that
it would map the
file system hierarchy.

I wrote a recursive function that would go through the file system

My problem is that a sub-node of a gtk.TreeStore is not a
gtk.TreeStore, but
a gtk.TreeIter. And gtk.treeter has almost no functions I would be
able to use (like "append")
Therefore I can not explore the whole file system.

Do you have another way so that I would be able to put all the file
system hierarchy
into a gtk.TreeStore?
I was quite happy with my recursive solution ...

code :
#!/usr/bin/env python
import pygtk
import gtk, gobject, os


class InfoModele:

	def matchPathTreetoModelTree(self, path, treeStoreNode):
		files = [f for f in os.listdir(path) if f[0] <> '.']
		for i in range(len(files)):
			if os.path.isfile(path+'/'+files[i]):
				print path+'/'+files[i]+" is a file"
				treeStoreNode.append( None, (path+'/'+files[i], None) )
			if os.path.isdir(path+'/'+files[i]): #is a directory, go
				print path+'/'+files[i]+" is a directory"
				mother = self.tree_store.append( None, (path+'/'+files[i], None) )
				self.matchPathTreetoModelTree(path+'/'+files[i], mother)

	def __init__(self):
		self.tree_store = gtk.TreeStore( gobject.TYPE_STRING,
		self.matchPathTreetoModelTree(prefdir, self.tree_store)

if __name__ == "__main__":

file system :

Program output :
/tmp/a/ab is a directory
/tmp/a/ab/abc is a file
Traceback (most recent call last):
  File "", line 28, in <module>
  File "", line 25, in __init__
    self.matchPathTreetoModelTree(prefdir, self.tree_store)
  File "", line 20, in matchPathTreetoModelTree
    self.matchPathTreetoModelTree(path+'/'+files[i], mother)
  File "", line 16, in matchPathTreetoModelTree
    treeStoreNode.append( None, (path+'/'+files[i], None) )
AttributeError: 'gtk.TreeIter' object has no attribute 'append'

More information about the Python-list mailing list