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

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


Hello,

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
tree.

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
pygtk.require('2.0')
import gtk, gobject, os


prefdir="/tmp/a"

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
recursively
				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,
gobject.TYPE_STRING)
		path=prefdir
		self.matchPathTreetoModelTree(prefdir, self.tree_store)

if __name__ == "__main__":
	i=InfoModele()

=================================
file system :
a
|-ab/
   |-abc/
   |-abcd

================================
Program output :
/tmp/a/ab is a directory
/tmp/a/ab/abc is a file
Traceback (most recent call last):
  File "question.py", line 28, in <module>
    i=InfoModele()
  File "question.py", line 25, in __init__
    self.matchPathTreetoModelTree(prefdir, self.tree_store)
  File "question.py", line 20, in matchPathTreetoModelTree
    self.matchPathTreetoModelTree(path+'/'+files[i], mother)
  File "question.py", 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