]>
Commit | Line | Data |
---|---|---|
be05b434 RD |
1 | import wx |
2 | import wx.gizmos | |
3 | import data | |
4 | ||
5 | class TestFrame(wx.Frame): | |
6 | def __init__(self): | |
7 | wx.Frame.__init__(self, None, title="TreeListCtrl", size=(400,500)) | |
8 | ||
9 | # Create an image list | |
10 | il = wx.ImageList(16,16) | |
11 | ||
12 | # Get some standard images from the art provider and add them | |
13 | # to the image list | |
14 | self.fldridx = il.Add( | |
15 | wx.ArtProvider.GetBitmap(wx.ART_FOLDER, wx.ART_OTHER, (16,16))) | |
16 | self.fldropenidx = il.Add( | |
17 | wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_OTHER, (16,16))) | |
18 | self.fileidx = il.Add( | |
19 | wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, wx.ART_OTHER, (16,16))) | |
20 | ||
21 | ||
22 | # Create the tree | |
23 | self.tree = wx.gizmos.TreeListCtrl(self, style = | |
24 | wx.TR_DEFAULT_STYLE | |
25 | | wx.TR_FULL_ROW_HIGHLIGHT) | |
26 | ||
27 | # Give it the image list | |
28 | self.tree.AssignImageList(il) | |
29 | ||
30 | ||
31 | # create some columns | |
32 | self.tree.AddColumn("Class Name") | |
33 | self.tree.AddColumn("Description") | |
34 | self.tree.SetMainColumn(0) # the one with the tree in it... | |
35 | self.tree.SetColumnWidth(0, 200) | |
36 | self.tree.SetColumnWidth(1, 200) | |
37 | ||
38 | # Add a root node and assign it some images | |
39 | root = self.tree.AddRoot("wx.Object") | |
40 | self.tree.SetItemText(root, "A description of wx.Object", 1) | |
41 | self.tree.SetItemImage(root, self.fldridx, | |
42 | wx.TreeItemIcon_Normal) | |
43 | self.tree.SetItemImage(root, self.fldropenidx, | |
44 | wx.TreeItemIcon_Expanded) | |
45 | ||
46 | # Add nodes from our data set | |
47 | self.AddTreeNodes(root, data.tree) | |
48 | ||
49 | # Bind some interesting events | |
50 | self.Bind(wx.EVT_TREE_ITEM_EXPANDED, self.OnItemExpanded, self.tree) | |
51 | self.Bind(wx.EVT_TREE_ITEM_COLLAPSED, self.OnItemCollapsed, self.tree) | |
52 | self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelChanged, self.tree) | |
53 | self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnActivated, self.tree) | |
54 | ||
55 | # Expand the first level | |
56 | self.tree.Expand(root) | |
57 | ||
58 | ||
59 | def AddTreeNodes(self, parentItem, items): | |
60 | """ | |
61 | Recursively traverses the data structure, adding tree nodes to | |
62 | match it. | |
63 | """ | |
64 | for item in items: | |
65 | if type(item) == str: | |
66 | newItem = self.tree.AppendItem(parentItem, item) | |
67 | self.tree.SetItemText(newItem, "A description of %s" % item, 1) | |
68 | self.tree.SetItemImage(newItem, self.fileidx, | |
69 | wx.TreeItemIcon_Normal) | |
70 | else: | |
71 | newItem = self.tree.AppendItem(parentItem, item[0]) | |
72 | self.tree.SetItemText(newItem, "A description of %s" % item[0], 1) | |
73 | self.tree.SetItemImage(newItem, self.fldridx, | |
74 | wx.TreeItemIcon_Normal) | |
75 | self.tree.SetItemImage(newItem, self.fldropenidx, | |
76 | wx.TreeItemIcon_Expanded) | |
77 | ||
78 | self.AddTreeNodes(newItem, item[1]) | |
79 | ||
80 | ||
81 | def GetItemText(self, item): | |
82 | if item: | |
83 | return self.tree.GetItemText(item) | |
84 | else: | |
85 | return "" | |
86 | ||
87 | def OnItemExpanded(self, evt): | |
88 | print "OnItemExpanded: ", self.GetItemText(evt.GetItem()) | |
89 | ||
90 | def OnItemCollapsed(self, evt): | |
91 | print "OnItemCollapsed:", self.GetItemText(evt.GetItem()) | |
92 | ||
93 | def OnSelChanged(self, evt): | |
94 | print "OnSelChanged: ", self.GetItemText(evt.GetItem()) | |
95 | ||
96 | def OnActivated(self, evt): | |
97 | print "OnActivated: ", self.GetItemText(evt.GetItem()) | |
98 | ||
99 | ||
100 | app = wx.PySimpleApp(redirect=True) | |
101 | frame = TestFrame() | |
102 | frame.Show() | |
103 | app.MainLoop() | |
104 |