Hello guys,
Please help me with wxTreeCtrl events (I have never dealt with TreeCtrl before). I want to get simple basics how to catch events

As example to clarify my question, I have tree of books and I want just the TextCtrl to display the chosen parent and Item, like "Biology 1" or "Physics 2". Main purpose is to learn TreeCtrl.

I have googled (and still checking google) and the demo+api, but haven't got in good place.
Thanks alot

import wx

class TestTree(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)
        #Menu & toolbar stuffs
        menubar = wx.MenuBar()
        filemenu = wx.Menu()
        filemenu.Append(wx.ID_EXIT, "Exit", "Exit this small app")
        self.Bind(wx.EVT_MENU, self.OnClose, id = wx.ID_EXIT)
        menubar.Append(filemenu, "File")
        self.SetMenuBar(menubar) 
        
        self.CreateToolBar()
        self.CreateStatusBar()
        self.SetStatusText("This small app")
        
        self.panel = wx.Panel(self, -1)
        
        self.books = wx.TreeCtrl(self.panel, -1, size = (120, 200), style = wx.TR_DEFAULT_STYLE|wx.TR_HIDE_ROOT)
        self.child = self.books.AddRoot("The Library")
        self.Populate()
        
        self.tc = wx.TextCtrl(self.panel, -1, style = wx.TE_MULTILINE|wx.NO_BORDER)
        
        hbox = wx.BoxSizer(wx.HORIZONTAL)
        hbox.Add(self.books, 0, wx.EXPAND)
        hbox.Add(self.tc, 1, wx.EXPAND)
        self.panel.SetSizer(hbox)
        self.Layout()
        
    def OnClose(self, evt):
        self.Close()
        
    def Populate(self):
        BooksList = ["Biology", "Physics", "History", "Computer", "Telecommunications"]
        for book in BooksList:
                j = self.books.AppendItem(self.child, book)
                index = BooksList.index(book)
                for k in range(1,5):
                    self.books.AppendItem(j, str(k))
            
        
        
if (__name__ == "__main__"):
    app = wx.App(False)
    f = TestTree(None, -1, "Testing tree control")
    f.Show()
    app.MainLoop()

I found example here by vega,
I'm lost alittle in the three(may be four) events on the same widget. I need some little light on the events so that I don't just copy and paste without knowing what I'm doing

Hi All,
so far I have managed to get Book number and type as shown below. The problem is, when all books are expanded and you jump to
let say Biology book 1 it shows the last parent. I wonder if there is easy way I can do to get a parent of currently selected items, like when I select
book 1 from biology then I get both Book 1 and its parent i.e biology

import wx

class TestTree(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)
        #Menu & toolbar stuffs
        menubar = wx.MenuBar()
        filemenu = wx.Menu()
        filemenu.Append(wx.ID_EXIT, "Exit", "Exit this small app")
        self.Bind(wx.EVT_MENU, self.OnClose, id = wx.ID_EXIT)
        menubar.Append(filemenu, "File")
        self.SetMenuBar(menubar) 
        
        self.CreateToolBar()
        self.CreateStatusBar()
        self.SetStatusText("This small app")
        
        self.panel = wx.Panel(self, -1)
        
        self.books = wx.TreeCtrl(self.panel, -1, size = (120, 200), style = wx.TR_DEFAULT_STYLE|wx.TR_HIDE_ROOT)
        self.child = self.books.AddRoot("The Library")
        self.Populate()
        #Hold current parent?
        self.bookparent = ""
        #Bind to events
        self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnChanged, self.books)
        self.Bind(wx.EVT_TREE_ITEM_EXPANDED, self.OnExpanded, self.books)
        
        self.tc = wx.TextCtrl(self.panel, -1, style = wx.TE_MULTILINE|wx.NO_BORDER)
        
        hbox = wx.BoxSizer(wx.HORIZONTAL)
        hbox.Add(self.books, 0, wx.EXPAND)
        hbox.Add(self.tc, 1, wx.EXPAND)
        self.panel.SetSizer(hbox)
        self.Layout()
        
    def OnExpanded(self, evt):
        selected = evt.GetItem()
        self.bookparent = self.books.GetItemText(selected)
    
    def OnChanged(self, evt):
        selected = evt.GetItem()
        text =  "\t Selected Book "+self.books.GetItemText(selected)+ " of type "+ self.bookparent+"\n"
        self.tc.WriteText(text)
        
        
    def OnClose(self, evt):
        self.Close()
        
    def Populate(self):
        BooksList = ["Biology", "Physics", "History", "Computer", "Telecommunications"]
        for book in BooksList:
                j = self.books.AppendItem(self.child, book)
                index = BooksList.index(book)
                for k in range(1,5):
                    self.books.AppendItem(j, str(k))
            
        
        
if (__name__ == "__main__"):
    app = wx.App(False)
    f = TestTree(None, -1, "Testing tree control")
    f.Show()
    app.MainLoop()

I got help form www.python-forum.org and I came up with this version of the program. Although it works, it throws errors:

Code

import wx

class TestTree(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)
        #Menu & toolbar stuffs
        menubar = wx.MenuBar()
        filemenu = wx.Menu()
        filemenu.Append(wx.ID_EXIT, "Exit", "Exit this small app")
        self.Bind(wx.EVT_MENU, self.OnClose, id = wx.ID_EXIT)
        menubar.Append(filemenu, "File")
        self.SetMenuBar(menubar)
       
        self.CreateToolBar()
        self.CreateStatusBar()
        self.SetStatusText("This small app")
       
        self.panel = wx.Panel(self, -1)
       
        self.books = wx.TreeCtrl(self.panel, -1, size = (120, 200), style = wx.TR_DEFAULT_STYLE|wx.TR_HIDE_ROOT)
        self.child = self.books.AddRoot("The Library")
        self.Populate()
        
        #Bind to events
        self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnChanged, self.books)
        self.Bind(wx.EVT_TREE_ITEM_EXPANDED, self.OnExpanded, self.books)
       
        self.tc = wx.TextCtrl(self.panel, -1, style = wx.TE_MULTILINE|wx.NO_BORDER)
       
        hbox = wx.BoxSizer(wx.HORIZONTAL)
        hbox.Add(self.books, 0, wx.EXPAND)
        hbox.Add(self.tc, 1, wx.EXPAND)
        self.panel.SetSizer(hbox)
        self.Layout()
       
    def OnExpanded(self, evt):
        selected = evt.GetItem()
        self.bookparent = self.books.GetItemText(selected)
   
    def OnChanged(self, evt):
        selected = evt.GetItem()
        parent = self.books.GetItemParent(selected)
        text =  "\t Selected Book "+self.books.GetItemText(selected)+ " of type "+ self.books.GetItemText(parent)+"\n"
        self.tc.WriteText(text)
       
       
    def OnClose(self, evt):
        self.Close()
       
    def Populate(self):
        BooksList = ["Biology", "Physics", "History", "Computer", "Telecommunications"]
        for book in BooksList:
                j = self.books.AppendItem(self.child, book)
                index = BooksList.index(book)
                for k in range(1,5):
                    self.books.AppendItem(j, str(k))
           
       
       
if (__name__ == "__main__"):
    app = wx.App(False)
    f = TestTree(None, -1, "Testing tree control")
    f.Show()
    app.MainLoop()

Error:
Traceback (most recent call last):
File "i:\Hosanna\Programmer Codes\Python\treectrl.py", line 43, in OnChanged
text = "\t Selected Book "+self.books.GetItemText(selected)+ " of type "+ self.books.GetItemText(parent)+"\n"
File "C:\Python25\Lib\site-packages\wx-2.8-msw-unicode\wx\_controls.py", line 5303, in GetItemText
return _controls_.TreeCtrl_GetItemText(*args, **kwargs)
wx._core.PyAssertionError: C++ assertion "tvItem->hItem != ((HTREEITEM)(ULONG_PTR)-0x10000)" failed at ..\..\src\msw\treectrl.cpp(854) in wxTreeCtrl::DoGetItem(): can't retrieve virtual root item

This article has been dead for over six months. Start a new discussion instead.