treectrl - getselection

Chris,

Chris Mahoney wrote:

Hi,

This is the first time I have felt the need to post to this list, but I have always found it a valuable resource. Thanks to all who contribute.

I have this function to add a branch to a treectrl at the selected item. I want it to raise an exception if nothing is selected, rather than just failing silently. However, the exception is only raised if the treectrl is completely empty. As soon as a root is added to the treectrl the exception isn't raised even when nothing is selected.

************************************

def addBranch(self, event):

   try:

       selection = self.treectrl1.GetSelection()

       branchNum = self.treectrl1.GetChildrenCount(selection, recursively = False)

       newBranch = self.treectrl1.AppendItem(selection, ("Branch %i" % branchNum))

       self.treectrl1.SetItemImage(newBranch, newBranchClose, wxTreeItemIcon_Normal)

       self.treectrl1.SetItemImage(newBranch, newBranchOpen, wxTreeItemIcon_Expanded)

       event.Skip()

   except:

       dlg = wxMessageDialog(self, "Cannot add branch without selection!", "Selection Error", style=wxOK | wxICON_ERROR)

       dlg.CenterOnScreen(wxBOTH)

       try:

           if dlg.ShowModal() == wxID_OK:

               event.Skip()

           finally: dlg.Destroy()

************************************

I am not sure about wxTreeListCtrl, but e.g. wxListCtrl returns "-1" if nothing is selected (no exception).

So, instead of trying to catch an exception why don't you do something along these lines.

selection = self.treectrl.GetSelection()
if selection == -1:
  do your error handling
else:
  do your real work

What am I missing? According to the documentation, "wxTreeCtrl::GetSelection...Returns the selection, or an invalid item if there is no selection." Shouldn't this be enough to raise the exception?

TIA,

Chris

_______________________________________________
Join Excite! - http://www.excite.com
The most personalized portal on the Web!

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org

see you
Werner