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
)
18 self
.root
= self
.tree
.AddRoot("The Root Item")
20 child
= self
.tree
.AppendItem(self
.root
, "Item %d" % x
)
22 last
= self
.tree
.AppendItem(child
, "item %d-%s" % (x
, chr(ord("a")+y
)))
24 self
.tree
.AppendItem(last
, "item %d-%s-%d" % (x
, chr(ord("a")+y
), z
))
26 self
.tree
.Expand(self
.root
)
27 EVT_TREE_ITEM_EXPANDED (self
, tID
, self
.OnItemExpanded
)
28 EVT_TREE_ITEM_COLLAPSED (self
, tID
, self
.OnItemCollapsed
)
29 EVT_TREE_SEL_CHANGED (self
, tID
, self
.OnSelChanged
)
30 EVT_TREE_BEGIN_LABEL_EDIT(self
, tID
, self
.OnBeginEdit
)
31 EVT_TREE_END_LABEL_EDIT (self
, tID
, self
.OnEndEdit
)
33 EVT_LEFT_DCLICK(self
.tree
, self
.OnLeftDClick
)
34 EVT_RIGHT_DOWN(self
.tree
, self
.OnRightClick
)
35 EVT_RIGHT_UP(self
.tree
, self
.OnRightUp
)
37 def OnRightClick(self
, event
):
38 (x
,y
) = event
.Position();
39 item
= self
.tree
.HitTest(wxPoint(x
,y
))
40 self
.log
.WriteText("OnRightClick: %s\n" % self
.tree
.GetItemText(item
))
41 self
.tree
.SelectItem(item
)
43 def OnRightUp(self
, event
):
44 (x
,y
) = event
.Position();
45 item
= self
.tree
.HitTest(wxPoint(x
,y
))
46 self
.log
.WriteText("OnRightUp: %s (manually starting label edit)\n"
47 % self
.tree
.GetItemText(item
))
48 self
.tree
.EditLabel(item
)
52 def OnBeginEdit(self
, event
):
53 self
.log
.WriteText("OnBeginEdit\n")
54 # show how to prevent edit...
55 if self
.tree
.GetItemText(event
.GetItem()) == "The Root Item":
57 self
.log
.WriteText("You can't edit this one...\n")
60 def OnEndEdit(self
, event
):
61 self
.log
.WriteText("OnEndEdit\n")
62 # show how to reject edit, we'll not allow any digits
63 for x
in event
.GetLabel():
64 if x
in string
.digits
:
65 self
.log
.WriteText("You can't enter digits...\n")
71 def OnLeftDClick(self
, event
):
72 (x
,y
) = event
.Position();
73 item
= self
.tree
.HitTest(wxPoint(x
,y
))
74 self
.log
.WriteText("OnLeftDClick: %s\n" % self
.tree
.GetItemText(item
))
77 def OnSize(self
, event
):
78 w
,h
= self
.GetClientSizeTuple()
79 self
.tree
.SetDimensions(0, 0, w
, h
)
82 def OnItemExpanded(self
, event
):
83 item
= event
.GetItem()
84 self
.log
.WriteText("OnItemExpanded: %s\n" % self
.tree
.GetItemText(item
))
86 def OnItemCollapsed(self
, event
):
87 item
= event
.GetItem()
88 self
.log
.WriteText("OnItemCollapsed: %s\n" % self
.tree
.GetItemText(item
))
90 def OnSelChanged(self
, event
):
91 self
.item
= event
.GetItem()
92 self
.log
.WriteText("OnSelChanged: %s\n" % self
.tree
.GetItemText(self
.item
))
94 #---------------------------------------------------------------------------
96 def runTest(frame
, nb
, log
):
97 win
= TestTreeCtrlPanel(nb
, log
)
100 #---------------------------------------------------------------------------
120 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.
123 -------------------------
127 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")
129 Constructor, creating and showing a tree control.
134 parent = Parent window. Must not be NULL.
136 id = Window identifier. A value of -1 indicates a default value.
138 pos = Window position.
140 size = Window size. If the default size (-1, -1) is specified then the window is sized appropriately.
142 style = Window style. See wxTreeCtrl.
144 validator = Window validator.