4 class TestFrame(wx
.Frame
):
6 wx
.Frame
.__init
__(self
, None, title
="tree: misc tests", size
=(400,500))
9 il
= wx
.ImageList(16,16)
11 # Get some standard images from the art provider and add them
13 self
.fldridx
= il
.Add(
14 wx
.ArtProvider
.GetBitmap(wx
.ART_FOLDER
, wx
.ART_OTHER
, (16,16)))
15 self
.fldropenidx
= il
.Add(
16 wx
.ArtProvider
.GetBitmap(wx
.ART_FILE_OPEN
, wx
.ART_OTHER
, (16,16)))
17 self
.fileidx
= il
.Add(
18 wx
.ArtProvider
.GetBitmap(wx
.ART_NORMAL_FILE
, wx
.ART_OTHER
, (16,16)))
22 self
.tree
= wx
.TreeCtrl(self
, style
=wx
.TR_DEFAULT_STYLE | wx
.TR_EDIT_LABELS
)
24 # Give it the image list
25 self
.tree
.AssignImageList(il
)
28 # Add a root node and assign it some images
29 root
= self
.tree
.AddRoot("wx.Object")
30 self
.tree
.SetItemPyData(root
, None)
31 self
.tree
.SetItemImage(root
, self
.fldridx
,
32 wx
.TreeItemIcon_Normal
)
33 self
.tree
.SetItemImage(root
, self
.fldropenidx
,
34 wx
.TreeItemIcon_Expanded
)
36 # Add nodes from our data set
37 self
.AddTreeNodes(root
, data
.tree
)
39 # Bind some interesting events
40 self
.Bind(wx
.EVT_TREE_ITEM_EXPANDED
, self
.OnItemExpanded
, self
.tree
)
41 self
.Bind(wx
.EVT_TREE_ITEM_COLLAPSED
, self
.OnItemCollapsed
, self
.tree
)
42 self
.Bind(wx
.EVT_TREE_SEL_CHANGED
, self
.OnSelChanged
, self
.tree
)
43 self
.Bind(wx
.EVT_TREE_ITEM_ACTIVATED
, self
.OnActivated
, self
.tree
)
45 self
.Bind(wx
.EVT_TREE_BEGIN_LABEL_EDIT
, self
.OnBeginEdit
, self
.tree
)
48 # Expand the first level
49 self
.tree
.Expand(root
)
53 mi
= menu
.Append(-1, "Edit Item")
54 self
.Bind(wx
.EVT_MENU
, self
.OnEditItem
, mi
)
55 mi
= menu
.Append(-1, "Sort Children")
56 self
.Bind(wx
.EVT_MENU
, self
.OnSortChildren
, mi
)
57 mi
= menu
.Append(-1, "Delete Children")
58 self
.Bind(wx
.EVT_MENU
, self
.OnDeleteChildren
, mi
)
59 mi
= menu
.Append(-1, "Find all Children")
60 self
.Bind(wx
.EVT_MENU
, self
.OnFindChildren
, mi
)
62 mb
.Append(menu
, "Tests")
66 def AddTreeNodes(self
, parentItem
, items
):
68 Recursively traverses the data structure, adding tree nodes to
73 newItem
= self
.tree
.AppendItem(parentItem
, item
)
74 self
.tree
.SetItemPyData(newItem
, None)
75 self
.tree
.SetItemImage(newItem
, self
.fileidx
,
76 wx
.TreeItemIcon_Normal
)
78 newItem
= self
.tree
.AppendItem(parentItem
, item
[0])
79 self
.tree
.SetItemPyData(newItem
, None)
80 self
.tree
.SetItemImage(newItem
, self
.fldridx
,
81 wx
.TreeItemIcon_Normal
)
82 self
.tree
.SetItemImage(newItem
, self
.fldropenidx
,
83 wx
.TreeItemIcon_Expanded
)
85 self
.AddTreeNodes(newItem
, item
[1])
88 def GetItemText(self
, item
):
90 return self
.tree
.GetItemText(item
)
94 def OnItemExpanded(self
, evt
):
95 print "OnItemExpanded: ", self
.GetItemText(evt
.GetItem())
97 def OnItemCollapsed(self
, evt
):
98 print "OnItemCollapsed:", self
.GetItemText(evt
.GetItem())
100 def OnSelChanged(self
, evt
):
101 print "OnSelChanged: ", self
.GetItemText(evt
.GetItem())
103 def OnActivated(self
, evt
):
104 print "OnActivated: ", self
.GetItemText(evt
.GetItem())
107 def OnBeginEdit(self
, evt
):
108 print "OnBeginEdit: ", self
.GetItemText(evt
.GetItem())
109 # we can prevent nodes from being edited, for example let's
110 # not let the root node be edited...
112 if item
== self
.tree
.GetRootItem():
114 print "*** Edit was vetoed!"
117 def OnEditItem(self
, evt
):
118 item
= self
.tree
.GetSelection()
120 self
.tree
.EditLabel(item
)
122 def OnSortChildren(self
, evt
):
123 item
= self
.tree
.GetSelection()
125 self
.tree
.SortChildren(item
)
127 def OnDeleteChildren(self
, evt
):
128 item
= self
.tree
.GetSelection()
130 self
.tree
.DeleteChildren(item
)
132 def OnFindChildren(self
, evt
):
133 item
= self
.tree
.GetSelection()
135 self
.PrintAllItems(item
)
137 def PrintAllItems(self
, parent
, indent
=0):
138 text
= self
.tree
.GetItemText(parent
)
139 print ' ' * indent
, text
141 item
, cookie
= self
.tree
.GetFirstChild(parent
)
143 if self
.tree
.ItemHasChildren(item
):
144 self
.PrintAllItems(item
, indent
)
146 text
= self
.tree
.GetItemText(item
)
147 print ' ' * indent
, text
148 item
, cookie
= self
.tree
.GetNextChild(parent
, cookie
)
151 app
= wx
.PySimpleApp(redirect
=True)