]>
Commit | Line | Data |
---|---|---|
be05b434 RD |
1 | import wx |
2 | import data | |
3 | ||
4 | class TestFrame(wx.Frame): | |
5 | def __init__(self): | |
6 | wx.Frame.__init__(self, None, title="simple tree", size=(400,500)) | |
7 | ||
8 | # Create the tree | |
9 | self.tree = wx.TreeCtrl(self) | |
10 | ||
11 | # Add a root node | |
12 | root = self.tree.AddRoot("wx.Object") | |
13 | ||
14 | # Add nodes from our data set | |
15 | self.AddTreeNodes(root, data.tree) | |
16 | ||
17 | # Bind some interesting events | |
18 | self.Bind(wx.EVT_TREE_ITEM_EXPANDED, self.OnItemExpanded, self.tree) | |
19 | self.Bind(wx.EVT_TREE_ITEM_COLLAPSED, self.OnItemCollapsed, self.tree) | |
20 | self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelChanged, self.tree) | |
21 | self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnActivated, self.tree) | |
22 | ||
23 | # Expand the first level | |
24 | self.tree.Expand(root) | |
25 | ||
26 | ||
27 | def AddTreeNodes(self, parentItem, items): | |
28 | """ | |
29 | Recursively traverses the data structure, adding tree nodes to | |
30 | match it. | |
31 | """ | |
32 | for item in items: | |
33 | if type(item) == str: | |
34 | self.tree.AppendItem(parentItem, item) | |
35 | else: | |
36 | newItem = self.tree.AppendItem(parentItem, item[0]) | |
37 | self.AddTreeNodes(newItem, item[1]) | |
38 | ||
39 | def GetItemText(self, item): | |
40 | if item: | |
41 | return self.tree.GetItemText(item) | |
42 | else: | |
43 | return "" | |
44 | ||
45 | def OnItemExpanded(self, evt): | |
46 | print "OnItemExpanded: ", self.GetItemText(evt.GetItem()) | |
47 | ||
48 | def OnItemCollapsed(self, evt): | |
49 | print "OnItemCollapsed:", self.GetItemText(evt.GetItem()) | |
50 | ||
51 | def OnSelChanged(self, evt): | |
52 | print "OnSelChanged: ", self.GetItemText(evt.GetItem()) | |
53 | ||
54 | def OnActivated(self, evt): | |
55 | print "OnActivated: ", self.GetItemText(evt.GetItem()) | |
56 | ||
57 | ||
58 | app = wx.PySimpleApp(redirect=True) | |
59 | frame = TestFrame() | |
60 | frame.Show() | |
61 | app.MainLoop() | |
62 |