2 from wxPython
.wx
import *
6 #---------------------------------------------------------------------------
8 class TestTreeCtrlPanel(wxPanel
):
9 def __init__(self
, parent
, log
):
10 wxPanel
.__init
__(self
, parent
, -1)
15 self
.tree
= wxTreeCtrl(self
, tID
, wxDefaultPosition
, wxDefaultSize
,
16 wxTR_HAS_BUTTONS | wxTR_EDIT_LABELS
)# | wxTR_MULTIPLE)
18 self
.root
= self
.tree
.AddRoot("The Root Item")
20 child
= self
.tree
.AppendItem(self
.root
, "Item %d" % x
)
21 #self.tree.SelectItem(child)
23 last
= self
.tree
.AppendItem(child
, "item %d-%s" % (x
, chr(ord("a")+y
)))
25 self
.tree
.AppendItem(last
, "item %d-%s-%d" % (x
, chr(ord("a")+y
), z
))
27 self
.tree
.Expand(self
.root
)
28 EVT_TREE_ITEM_EXPANDED (self
, tID
, self
.OnItemExpanded
)
29 EVT_TREE_ITEM_COLLAPSED (self
, tID
, self
.OnItemCollapsed
)
30 EVT_TREE_SEL_CHANGED (self
, tID
, self
.OnSelChanged
)
31 EVT_TREE_BEGIN_LABEL_EDIT(self
, tID
, self
.OnBeginEdit
)
32 EVT_TREE_END_LABEL_EDIT (self
, tID
, self
.OnEndEdit
)
34 EVT_LEFT_DCLICK(self
.tree
, self
.OnLeftDClick
)
35 EVT_RIGHT_DOWN(self
.tree
, self
.OnRightClick
)
36 EVT_RIGHT_UP(self
.tree
, self
.OnRightUp
)
38 def OnRightClick(self
, event
):
39 pt
= event
.GetPosition();
40 item
= self
.tree
.HitTest(pt
)
41 self
.log
.WriteText("OnRightClick: %s\n" % self
.tree
.GetItemText(item
))
42 self
.tree
.SelectItem(item
)
44 def OnRightUp(self
, event
):
45 pt
= event
.GetPosition();
46 item
= self
.tree
.HitTest(pt
)
47 self
.log
.WriteText("OnRightUp: %s (manually starting label edit)\n"
48 % self
.tree
.GetItemText(item
))
49 self
.tree
.EditLabel(item
)
53 def OnBeginEdit(self
, event
):
54 self
.log
.WriteText("OnBeginEdit\n")
55 # show how to prevent edit...
56 if self
.tree
.GetItemText(event
.GetItem()) == "The Root Item":
58 self
.log
.WriteText("You can't edit this one...\n")
61 def OnEndEdit(self
, event
):
62 self
.log
.WriteText("OnEndEdit\n")
63 # show how to reject edit, we'll not allow any digits
64 for x
in event
.GetLabel():
65 if x
in string
.digits
:
66 self
.log
.WriteText("You can't enter digits...\n")
72 def OnLeftDClick(self
, event
):
73 pt
= event
.GetPosition();
74 item
= self
.tree
.HitTest(pt
)
75 self
.log
.WriteText("OnLeftDClick: %s\n" % self
.tree
.GetItemText(item
))
78 def OnSize(self
, event
):
79 w
,h
= self
.GetClientSizeTuple()
80 self
.tree
.SetDimensions(0, 0, w
, h
)
83 def OnItemExpanded(self
, event
):
84 item
= event
.GetItem()
85 self
.log
.WriteText("OnItemExpanded: %s\n" % self
.tree
.GetItemText(item
))
87 def OnItemCollapsed(self
, event
):
88 item
= event
.GetItem()
89 self
.log
.WriteText("OnItemCollapsed: %s\n" % self
.tree
.GetItemText(item
))
91 def OnSelChanged(self
, event
):
92 self
.item
= event
.GetItem()
93 self
.log
.WriteText("OnSelChanged: %s\n" % self
.tree
.GetItemText(self
.item
))
95 #---------------------------------------------------------------------------
97 def runTest(frame
, nb
, log
):
98 win
= TestTreeCtrlPanel(nb
, log
)
101 #---------------------------------------------------------------------------
121 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.
124 -------------------------
128 wxTreeCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxTR_HAS_BUTTONS, const wxValidator& validator = wxDefaultValidator, const wxString& name = "listCtrl")
130 Constructor, creating and showing a tree control.
135 parent = Parent window. Must not be NULL.
137 id = Window identifier. A value of -1 indicates a default value.
139 pos = Window position.
141 size = Window size. If the default size (-1, -1) is specified then the window is sized appropriately.
143 style = Window style. See wxTreeCtrl.
145 validator = Window validator.