1
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
2
# See LICENSE for details.
5
What I want it to look like:
24
Do whatever you want here.
29
Return the name of this node in the tree.
32
def isExpandable(self):
34
Return true if this node is expandable.
36
return len(self.getSubNodes())>0
37
def getSubNodes(self):
39
Return the sub nodes of this node.
42
def gotDoubleClick(self):
44
Called when we are double clicked.
49
Call me when something about me changes, so that my representation
56
def __init__(self,name):
60
return os.path.basename(self.name)
61
def isExpandable(self):
62
return os.path.isdir(self.name)
63
def getSubNodes(self):
64
names=map(lambda x,n=self.name:os.path.join(n,x),os.listdir(self.name))
65
return map(FileNode,names)
68
def __init__(self,widget,parent,node):
72
if self.node.isExpandable():
78
self.level=self.parent.level+1
81
self.first=0 # gets set in Tree.expand()
87
return "<Item for Node %s at level %s>"%(self.node.getName(),self.level)
90
Override in a subclass.
92
raise NotImplementedError
94
self.widget.update(self)
96
class ListboxTreeItem(TreeItem):
99
if self.expand==None and not self.first:
107
r=[start+"- "+self.node.getName()]
109
for i in self.subitems:
114
def __init__(self,parent=None,**options):
115
self.box=apply(Listbox,[parent],options)
116
self.box.bind("<Double-1>",self.flip)
119
def pack(self,*args,**kw):
123
apply(self.box.pack,args,kw)
124
def grid(self,*args,**kw):
128
apply(self.box.grid,args,kw)
129
def yview(self,*args,**kw):
133
apply(self.box.yview,args,kw)
134
def addRoot(self,node):
135
r=ListboxTreeItem(self,None,node)
138
self.box.insert(END,r.render()[0])
140
def curselection(self):
141
c=self.box.curselection()
143
return self.items[int(c[0])]
145
if not self.box.curselection(): return
146
item=self.items[int(self.box.curselection()[0])]
147
if item.expand==None: return
152
item.node.gotDoubleClick()
153
def expand(self,item):
154
if item.expand or item.expand==None: return
156
item.subitems=map(lambda x,i=item,s=self:ListboxTreeItem(s,i,x),item.node.getSubNodes())
158
item.subitems[0].first=1
159
i=self.items.index(item)
160
self.items,after=self.items[:i+1],self.items[i+1:]
161
self.items=self.items+item.subitems+after
162
c=self.items.index(item)
168
def close(self,item):
169
if not item.expand: return
171
length=len(item.subitems)
172
for i in item.subitems:
174
c=self.items.index(item)
175
del self.items[c+1:c+1+length]
176
for i in range(length+1):
178
self.box.insert(c,item.render()[0])
179
def remove(self,item):
182
c=self.items.index(item)
185
item.parent.subitems.remove(item)
187
def update(self,item):
188
if item.expand==None:
189
c=self.items.index(item)
191
self.box.insert(c,item.render()[0])
196
if __name__=="__main__":
199
t=ListboxTree(tk,yscrollcommand=s.set)
200
t.pack(side=LEFT,fill=BOTH)
201
s.config(command=t.yview)
202
s.pack(side=RIGHT,fill=Y)
203
t.addRoot(FileNode("C:/"))