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 il
= wxImageList(16, 16)
19 idx1
= il
.Add(wxBitmap('bitmaps/smiles.bmp', wxBITMAP_TYPE_BMP
))
20 idx2
= il
.Add(wxBitmap('bitmaps/open.bmp', wxBITMAP_TYPE_BMP
))
21 idx3
= il
.Add(wxBitmap('bitmaps/new.bmp', wxBITMAP_TYPE_BMP
))
22 idx4
= il
.Add(wxBitmap('bitmaps/copy.bmp', wxBITMAP_TYPE_BMP
))
23 idx5
= il
.Add(wxBitmap('bitmaps/paste.bmp', wxBITMAP_TYPE_BMP
))
25 self
.tree
.SetImageList(il
)
28 self
.root
= self
.tree
.AddRoot("The Root Item")
29 self
.tree
.SetItemImage(self
.root
, idx1
)
32 child
= self
.tree
.AppendItem(self
.root
, "Item %d" % x
)
33 self
.tree
.SetItemImage(child
, idx2
)
34 self
.tree
.SetItemSelectedImage(child
, idx3
)
36 last
= self
.tree
.AppendItem(child
, "item %d-%s" % (x
, chr(ord("a")+y
)))
37 self
.tree
.SetItemImage(last
, idx4
)
38 self
.tree
.SetItemSelectedImage(last
, idx5
)
40 self
.tree
.AppendItem(last
, "item %d-%s-%d" % (x
, chr(ord("a")+y
), z
))
42 self
.tree
.Expand(self
.root
)
43 EVT_TREE_ITEM_EXPANDED (self
, tID
, self
.OnItemExpanded
)
44 EVT_TREE_ITEM_COLLAPSED (self
, tID
, self
.OnItemCollapsed
)
45 EVT_TREE_SEL_CHANGED (self
, tID
, self
.OnSelChanged
)
46 EVT_TREE_BEGIN_LABEL_EDIT(self
, tID
, self
.OnBeginEdit
)
47 EVT_TREE_END_LABEL_EDIT (self
, tID
, self
.OnEndEdit
)
49 EVT_LEFT_DCLICK(self
.tree
, self
.OnLeftDClick
)
50 EVT_RIGHT_DOWN(self
.tree
, self
.OnRightClick
)
51 EVT_RIGHT_UP(self
.tree
, self
.OnRightUp
)
53 def OnRightClick(self
, event
):
54 pt
= event
.GetPosition();
55 item
= self
.tree
.HitTest(pt
)
56 self
.log
.WriteText("OnRightClick: %s\n" % self
.tree
.GetItemText(item
))
57 self
.tree
.SelectItem(item
)
59 def OnRightUp(self
, event
):
60 pt
= event
.GetPosition();
61 item
= self
.tree
.HitTest(pt
)
62 self
.log
.WriteText("OnRightUp: %s (manually starting label edit)\n"
63 % self
.tree
.GetItemText(item
))
64 self
.tree
.EditLabel(item
)
68 def OnBeginEdit(self
, event
):
69 self
.log
.WriteText("OnBeginEdit\n")
70 # show how to prevent edit...
71 if self
.tree
.GetItemText(event
.GetItem()) == "The Root Item":
73 self
.log
.WriteText("You can't edit this one...\n")
76 def OnEndEdit(self
, event
):
77 self
.log
.WriteText("OnEndEdit\n")
78 # show how to reject edit, we'll not allow any digits
79 for x
in event
.GetLabel():
80 if x
in string
.digits
:
81 self
.log
.WriteText("You can't enter digits...\n")
87 def OnLeftDClick(self
, event
):
88 pt
= event
.GetPosition();
89 item
= self
.tree
.HitTest(pt
)
90 self
.log
.WriteText("OnLeftDClick: %s\n" % self
.tree
.GetItemText(item
))
93 def OnSize(self
, event
):
94 w
,h
= self
.GetClientSizeTuple()
95 self
.tree
.SetDimensions(0, 0, w
, h
)
98 def OnItemExpanded(self
, event
):
99 item
= event
.GetItem()
100 self
.log
.WriteText("OnItemExpanded: %s\n" % self
.tree
.GetItemText(item
))
102 def OnItemCollapsed(self
, event
):
103 item
= event
.GetItem()
104 self
.log
.WriteText("OnItemCollapsed: %s\n" % self
.tree
.GetItemText(item
))
106 def OnSelChanged(self
, event
):
107 self
.item
= event
.GetItem()
108 self
.log
.WriteText("OnSelChanged: %s\n" % self
.tree
.GetItemText(self
.item
))
110 #---------------------------------------------------------------------------
112 def runTest(frame
, nb
, log
):
113 win
= TestTreeCtrlPanel(nb
, log
)
116 #---------------------------------------------------------------------------
136 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.
139 -------------------------
143 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")
145 Constructor, creating and showing a tree control.
150 parent = Parent window. Must not be NULL.
152 id = Window identifier. A value of -1 indicates a default value.
154 pos = Window position.
156 size = Window size. If the default size (-1, -1) is specified then the window is sized appropriately.
158 style = Window style. See wxTreeCtrl.
160 validator = Window validator.