Newbie: How to expand a tree node by using a tree event?

Hi,

I have a TreeCtrl object where the tree items are appended only when a
node is expanded. So what I did was to create a function OnItemExpand
that adds tree items and make this as the handler for
EVT_TREE_ITEM_EXPANDED.

At the initial loading, the root is added and expanded by calling
'Expand'. The tree items under the root are added by calling another
function. My code goes something like this:

class MyTreeCtrl(wxTreeCtrl):
     def __init__(self,....):
            :
        tID = wxNewId()
        wxTreeCtrl.__init__(self,parent,......)

        self.root=self.AddRoot(rootItem)
        self.Expand(self.root)
        self.AddItems(..)

        EVT_TREE_ITEM_EXPANDED(self,tID,self.OnItemExpanded)

     def AddItems(self,...):
              :
              :

     def OnItemExpanded(self,..):
         item=event.GetItem()
         text=self.GetItemText(item)
                :
         self.AddItems(...)

I want to simplify __init__ part by generating a TREE_ITEM_EXPANDED
event instead. So what I did was I get rid of the call to the
self.Expand and self.AddItems functions and added the following calls
instead:

          evt=wxTreeEvent(wxEVT_COMMAND_TREE_ITEM_EXPANDED,tID)
          self.GetEventHandler().ProcessEvent(evt)

My problems now are:

(1) The event is sent but the tree is not expanding.
(2) I am getting an error "assert "wxFalse" failed: invalid tree item"
during the call to GetItemText(item) under OnItemExpanded.
(3) How can I specify which node I want to expand using this approach
(i.e. if this is possible at all)?

Hope you can help me on this.

Thanks.

AL

Hi All,

It seems that wxDb (etc) are not supported in wxPython - am I correct or just not looking in the right place?

If this is correct, what are the plans to support wxDb (ODBC stuff) in wxPython.

Appreciate any hints on alternatives.

Best regards
Werner

···

Alfredo P. Ricafort wrote:

Hi,

I have a TreeCtrl object where the tree items are appended only when a
node is expanded. So what I did was to create a function OnItemExpand
that adds tree items and make this as the handler for
EVT_TREE_ITEM_EXPANDED.

At the initial loading, the root is added and expanded by calling
'Expand'. The tree items under the root are added by calling another
function. My code goes something like this:

class MyTreeCtrl(wxTreeCtrl):
     def __init__(self,....): : tID = wxNewId()
        wxTreeCtrl.__init__(self,parent,......)

        self.root=self.AddRoot(rootItem)
        self.Expand(self.root)
        self.AddItems(..)

        EVT_TREE_ITEM_EXPANDED(self,tID,self.OnItemExpanded)

     def AddItems(self,...):
              :

     def OnItemExpanded(self,..):
         item=event.GetItem()
         text=self.GetItemText(item)
                :
         self.AddItems(...)

I want to simplify __init__ part by generating a TREE_ITEM_EXPANDED
event instead. So what I did was I get rid of the call to the
self.Expand and self.AddItems functions and added the following calls
instead:

          evt=wxTreeEvent(wxEVT_COMMAND_TREE_ITEM_EXPANDED,tID)
          self.GetEventHandler().ProcessEvent(evt)

My problems now are:

(1) The event is sent but the tree is not expanding.

The sending of the event does not cause the item to be expanded, rather normally the event is sent because the item was expanded. So you'll still need to cause the item to be expanded.

(2) I am getting an error "assert "wxFalse" failed: invalid tree item"
during the call to GetItemText(item) under OnItemExpanded.

Because the item in the event object is not valid.

(3) How can I specify which node I want to expand using this approach
(i.e. if this is possible at all)?

You can't. There are no public methods in wxTreeEvent for setting the tree item and other members.

Hope you can help me on this.

You'll probably want to stick with your original approach.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!