wx.TreeCtrl error!

Hi All,
I have tree of books and I want just the TextCtrl to display the
chosen parent and Item, like "Biology 1" or "Physics 2". So far my
code works but with error below. I'm lost and don't know where to go
from here.

I appreciate your efforts
Steve

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()

Traceback (most recent call last):
  File "i:\Documents\Hosanna\Programmer Projects\Python
\testing_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

You can’t do GetItemText() for the root item of a tree when using HIDE_ROOT.

So, try checking to see if “parent” is the tree root before constructing the text. Something like:

if parent == self.books.GetRootItem():

  --- do something for a category type item
  --- that does not go after 'parent' attributes

else:
— do what you are doing now for books

Don't forget you can store data with each tree item...you might want
to save some info with the actual entry with something like SetPyData
({'type': 'wxpython', 'author': 'dunn'}} :wink:

Just remember to use the python versions GetPyData and SetPyData

You can also set it when you create the item using AppendItem(self,
parent, text, image, selectedImage, data=wx.TreeItemData( pyobject ))

If all you need is the parent's text though what you're doing will
work fine...

- michael

···

On Sep 23, 5:24 pm, Mike Conley <mconl...@gmail.com> wrote:

You can't do GetItemText() for the root item of a tree when using HIDE_ROOT.

So, try checking to see if "parent" is the tree root before constructing the
text. Something like:

if parent == self.books.GetRootItem():
--- do something for a category type item
--- that does not go after 'parent' attributes
else:
--- do what you are doing now for books

Thanks guys!

···

On Sep 24, 5:18 am, michael h <michaelke...@gmail.com> wrote:

Don't forget you can store data with each tree item...you might want
to save some info with the actual entry with something like SetPyData
({'type': 'wxpython', 'author': 'dunn'}} :wink:

Just remember to use the python versions GetPyData and SetPyData

You can also set it when you create the item using AppendItem(self,
parent, text, image, selectedImage, data=wx.TreeItemData( pyobject ))

If all you need is the parent's text though what you're doing will
work fine...

- michael

On Sep 23, 5:24 pm, Mike Conley <mconl...@gmail.com> wrote:

> You can't do GetItemText() for the root item of a tree when using HIDE_ROOT.

> So, try checking to see if "parent" is the tree root before constructing the
> text. Something like:

> if parent == self.books.GetRootItem():
> --- do something for a category type item
> --- that does not go after 'parent' attributes
> else:
> --- do what you are doing now for books