Hi,
Using wxPython I'm deriving a class from the wxTreeCtrl class, to make it list sub-directory trees. I'd like to make it read the directories to display all by itself, so I don't need to clutter up the code of the wxFrame class with all kinds of control-specific things.
I started out with a wxTreeCtrl-example that comes with wxPython, and stripped it. Than I moved the code for displaying some items in the tree to my class derived from wxTreeCtrl, which was easy.
Now I'd like to have my custom tree-control also do things when some (of its own) events occur. And I can't seem to find out how to that. Is it possible? If so, will it still be possible for a wxPanel (the parent) to get the same event? Instead of the "EVT_"-construct, is there some way to overload a function that will get called when the event occurs? If so, where could I find information about which functions to overload?
Below my code so far (largely taken from the wxTreeCtrl example from the wxPython package).
Code:
#!/usr/bin/env python
from wxPython.wx import *
import string
class SubdirTreeCtrl(wxTreeCtrl):
def __init__(self, parent, id, pos, size):
wxTreeCtrl.__init__(self, parent, id, pos, size, wxTR_HAS_BUTTONS | wxTR_HIDE_ROOT)
self.root = self.AddRoot("Subdirectories")
# Add items
for x in range(15):
child = self.AppendItem(self.root, "Dir %d" % x)
self.SortChildren(self.root)
for y in range(5):
last = self.AppendItem(child, "File %d-%s" % (x, chr(ord("a")+y)))
self.Expand(self.root)
class MainPanel(wxPanel):
def __init__(self, parent):
wxPanel.__init__(self, parent, -1)
tID = wxNewId()
self.tree = SubdirTreeCtrl(self, tID, wxDefaultPosition, (200, 200))
# I'd like to move lines like the one below to the SubDirTreeCtrl class
EVT_TREE_ITEM_EXPANDED (self, tID, self.OnItemExpanded)
def OnItemExpanded(self, event):
item = event.GetItem()
print "OnItemExpanded: %s\n" % self.tree.GetItemText(item)
app = wxPySimpleApp()
frame = wxFrame(None, -1, "Directories")
MainPanel(frame)
frame.Show(true)
app.MainLoop()