2 from wxPython
.wx
import *
6 #---------------------------------------------------------------------------
8 class MyTreeCtrl(wxTreeCtrl
):
9 def __init__(self
, parent
, id, pos
, size
, style
, log
):
10 wxTreeCtrl
.__init
__(self
, parent
, id, pos
, size
, style
)
13 def OnCompareItems(self
, item1
, item2
):
14 t1
= self
.GetItemText(item1
)
15 t2
= self
.GetItemText(item2
)
16 self
.log
.WriteText('compare: ' + t1
+ ' <> ' + t2
+ '\n')
21 #---------------------------------------------------------------------------
23 class TestTreeCtrlPanel(wxPanel
):
24 def __init__(self
, parent
, log
):
25 # Use the WANTS_CHARS style so the panel doesn't eat the Return key.
26 wxPanel
.__init
__(self
, parent
, -1, style
=wxWANTS_CHARS
)
27 EVT_SIZE(self
, self
.OnSize
)
32 self
.tree
= MyTreeCtrl(self
, tID
, wxDefaultPosition
, wxDefaultSize
,
33 wxTR_HAS_BUTTONS | wxTR_EDIT_LABELS
# | wxTR_MULTIPLE
39 ##il = wxImageList(16, 16)
40 ##idx1 = il.Add(images.getSmilesBitmap())
41 ##idx2 = il.Add(images.getOpenBitmap())
42 ##idx3 = il.Add(images.getNewBitmap())
43 ##idx4 = il.Add(images.getCopyBitmap())
44 ##idx5 = il.Add(images.getPasteBitmap())
46 ##self.tree.SetImageList(il)
49 # NOTE: For some reason tree items have to have a data object in
50 # order to be sorted. Since our compare just uses the labels
51 # we don't need any real data, so we'll just use None.
53 self
.root
= self
.tree
.AddRoot("The Root Item")
54 self
.tree
.SetPyData(self
.root
, None)
55 ##self.tree.SetItemImage(self.root, idx1)
58 child
= self
.tree
.AppendItem(self
.root
, "Item %d" % x
)
59 self
.tree
.SetPyData(child
, None)
60 ##self.tree.SetItemImage(child, idx2, wxTreeItemIcon_Expanded)
61 ##self.tree.SetItemSelectedImage(child, idx3)
63 last
= self
.tree
.AppendItem(child
, "item %d-%s" % (x
, chr(ord("a")+y
)))
64 self
.tree
.SetPyData(last
, None)
65 ##self.tree.SetItemImage(last, idx4)
66 ##self.tree.SetItemSelectedImage(last, idx5)
68 item
= self
.tree
.AppendItem(last
, "item %d-%s-%d" % (x
, chr(ord("a")+y
), z
))
69 self
.tree
.SetPyData(item
, None)
71 self
.tree
.Expand(self
.root
)
72 EVT_TREE_ITEM_EXPANDED (self
, tID
, self
.OnItemExpanded
)
73 EVT_TREE_ITEM_COLLAPSED (self
, tID
, self
.OnItemCollapsed
)
74 EVT_TREE_SEL_CHANGED (self
, tID
, self
.OnSelChanged
)
75 EVT_TREE_BEGIN_LABEL_EDIT(self
, tID
, self
.OnBeginEdit
)
76 EVT_TREE_END_LABEL_EDIT (self
, tID
, self
.OnEndEdit
)
77 EVT_TREE_ITEM_ACTIVATED (self
, tID
, self
.OnActivate
)
79 EVT_LEFT_DCLICK(self
.tree
, self
.OnLeftDClick
)
80 EVT_RIGHT_DOWN(self
.tree
, self
.OnRightClick
)
81 EVT_RIGHT_UP(self
.tree
, self
.OnRightUp
)
85 def OnRightClick(self
, event
):
86 pt
= event
.GetPosition();
87 item
, flags
= self
.tree
.HitTest(pt
)
88 self
.log
.WriteText("OnRightClick: %s, %s, %s\n" %
89 (self
.tree
.GetItemText(item
), type(item
), item
.__class
__))
90 self
.tree
.SelectItem(item
)
93 def OnRightUp(self
, event
):
94 pt
= event
.GetPosition();
95 item
, flags
= self
.tree
.HitTest(pt
)
96 self
.log
.WriteText("OnRightUp: %s (manually starting label edit)\n"
97 % self
.tree
.GetItemText(item
))
98 self
.tree
.EditLabel(item
)
102 def OnBeginEdit(self
, event
):
103 self
.log
.WriteText("OnBeginEdit\n")
104 # show how to prevent edit...
105 if self
.tree
.GetItemText(event
.GetItem()) == "The Root Item":
107 self
.log
.WriteText("You can't edit this one...\n")
110 def OnEndEdit(self
, event
):
111 self
.log
.WriteText("OnEndEdit\n")
112 # show how to reject edit, we'll not allow any digits
113 for x
in event
.GetLabel():
114 if x
in string
.digits
:
115 self
.log
.WriteText("You can't enter digits...\n")
120 def OnLeftDClick(self
, event
):
121 pt
= event
.GetPosition();
122 item
, flags
= self
.tree
.HitTest(pt
)
123 self
.log
.WriteText("OnLeftDClick: %s\n" % self
.tree
.GetItemText(item
))
124 parent
= self
.tree
.GetItemParent(item
)
125 self
.tree
.SortChildren(parent
)
129 def OnSize(self
, event
):
130 w
,h
= self
.GetClientSizeTuple()
131 self
.tree
.SetDimensions(0, 0, w
, h
)
134 def OnItemExpanded(self
, event
):
135 item
= event
.GetItem()
136 self
.log
.WriteText("OnItemExpanded: %s\n" % self
.tree
.GetItemText(item
))
138 def OnItemCollapsed(self
, event
):
139 item
= event
.GetItem()
140 self
.log
.WriteText("OnItemCollapsed: %s\n" % self
.tree
.GetItemText(item
))
142 def OnSelChanged(self
, event
):
143 self
.item
= event
.GetItem()
144 self
.log
.WriteText("OnSelChanged: %s\n" % self
.tree
.GetItemText(self
.item
))
145 if wxPlatform
== '__WXMSW__':
146 self
.log
.WriteText("BoundingRect: %s\n" %
147 self
.tree
.GetBoundingRect(self
.item
, true
))
148 #items = self.tree.GetSelections()
149 #print map(self.tree.GetItemText, items)
153 def OnActivate(self
, evt
):
154 self
.log
.WriteText("OnActivate: %s\n" % self
.tree
.GetItemText(self
.item
))
157 #---------------------------------------------------------------------------
159 def runTest(frame
, nb
, log
):
160 win
= TestTreeCtrlPanel(nb
, log
)
163 #---------------------------------------------------------------------------
170 A tree control presents information as a hierarchy, with items that may be expanded to show further items. Items in a tree control are referenced by wxTreeItemId handles.
176 if __name__
== '__main__':
179 run
.main(['', os
.path
.basename(sys
.argv
[0])])