]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/wxPIA_book/Chapter-15/tree_virtual.py
4 class TestFrame(wx
.Frame
):
6 wx
.Frame
.__init
__(self
, None, title
="virtual tree with icons", size
=(400,500))
7 il
= wx
.ImageList(16,16)
9 wx
.ArtProvider
.GetBitmap(wx
.ART_FOLDER
, wx
.ART_OTHER
, (16,16)))
10 self
.fldropenidx
= il
.Add(
11 wx
.ArtProvider
.GetBitmap(wx
.ART_FILE_OPEN
, wx
.ART_OTHER
, (16,16)))
12 self
.fileidx
= il
.Add(
13 wx
.ArtProvider
.GetBitmap(wx
.ART_NORMAL_FILE
, wx
.ART_OTHER
, (16,16)))
14 self
.tree
= wx
.TreeCtrl(self
)
15 self
.tree
.AssignImageList(il
)
16 root
= self
.tree
.AddRoot("wx.Object")
17 self
.tree
.SetItemImage(root
, self
.fldridx
,
18 wx
.TreeItemIcon_Normal
)
19 self
.tree
.SetItemImage(root
, self
.fldropenidx
,
20 wx
.TreeItemIcon_Expanded
)
22 # Instead of adding nodes for the whole tree, just attach some
23 # data to the root node so that it can find and add its child
24 # nodes when it is expanded, and mark it as having children so
25 # it will be expandable.
26 self
.tree
.SetItemPyData(root
, data
.tree
)
27 self
.tree
.SetItemHasChildren(root
, True)
29 # Bind some interesting events
30 self
.Bind(wx
.EVT_TREE_ITEM_EXPANDED
, self
.OnItemExpanded
, self
.tree
)
31 self
.Bind(wx
.EVT_TREE_ITEM_COLLAPSED
, self
.OnItemCollapsed
, self
.tree
)
32 self
.Bind(wx
.EVT_TREE_SEL_CHANGED
, self
.OnSelChanged
, self
.tree
)
33 self
.Bind(wx
.EVT_TREE_ITEM_ACTIVATED
, self
.OnActivated
, self
.tree
)
35 self
.Bind(wx
.EVT_TREE_ITEM_EXPANDING
, self
.OnItemExpanding
, self
.tree
)
36 self
.tree
.Expand(root
)
39 def AddTreeNodes(self
, parentItem
):
41 Add nodes for just the children of the parentItem
43 items
= self
.tree
.GetItemPyData(parentItem
)
47 newItem
= self
.tree
.AppendItem(parentItem
, item
)
48 self
.tree
.SetItemImage(newItem
, self
.fileidx
,
49 wx
.TreeItemIcon_Normal
)
51 # this item has children
52 newItem
= self
.tree
.AppendItem(parentItem
, item
[0])
53 self
.tree
.SetItemImage(newItem
, self
.fldridx
,
54 wx
.TreeItemIcon_Normal
)
55 self
.tree
.SetItemImage(newItem
, self
.fldropenidx
,
56 wx
.TreeItemIcon_Expanded
)
57 self
.tree
.SetItemPyData(newItem
, item
[1])
58 self
.tree
.SetItemHasChildren(newItem
, True)
62 def GetItemText(self
, item
):
64 return self
.tree
.GetItemText(item
)
68 def OnItemExpanded(self
, evt
):
69 print "OnItemExpanded: ", self
.GetItemText(evt
.GetItem())
71 def OnItemExpanding(self
, evt
):
72 # When the item is about to be expanded add the first level of child nodes
73 print "OnItemExpanding:", self
.GetItemText(evt
.GetItem())
74 self
.AddTreeNodes(evt
.GetItem())
76 def OnItemCollapsed(self
, evt
):
77 print "OnItemCollapsed:", self
.GetItemText(evt
.GetItem())
78 # And remove them when collapsed as we don't need them any longer
79 self
.tree
.DeleteChildren(evt
.GetItem())
81 def OnSelChanged(self
, evt
):
82 print "OnSelChanged: ", self
.GetItemText(evt
.GetItem())
84 def OnActivated(self
, evt
):
85 print "OnActivated: ", self
.GetItemText(evt
.GetItem())
88 app
= wx
.PySimpleApp(redirect
=True)