[wxPython-users] More Questions On wx.TreeCtrl

Robin Dunn robin at alldunn.com
Thu Nov 2 20:40:38 PST 2006


Rich Shepard wrote:

>   This TreeCtrl allows users to enter items and sub-items (all under the
> same root). However, if a user tries to enter a sub-item when there is no
> parent item, an error message should appear rather than the text entry
> dialog box. I don't have the syntax correct to do this. The following does
> not work:
> 
>   def OnSubPolAdd(self, event):
>     newSubPol = wx.TextEntryDialog(self, 'Add Sub-Policy name:',
>                                 'Sub-Policy Name', 
> style=wx.OK|wx.CANCEL|wx.CENTER)
>     txt = None
>     parent = None
>     if (newSubPol.GetItem() == None):
>       result = wx.MessageDialog(self, parent, 'No policy for a 
> sub-policy.', 'Sub-Policy Add Failed',wx.OK | wx.ICON_ERROR)
>       result.ShowModal()
>       result.Destroy()
>     if (newSubPol.ShowModal() == wx.ID_OK):
>       txt = newSubPol.GetValue()
>       subPolID = self.polTree.AppendItem(self.polID, 'txt')
>       newSubPol.Destroy()
> 
> Python's error message is, "AttributeError: 'TextEntryDialog' object has no
> attribute 'GetItem'" and I get a seg fault.

The problem is exactly what the error says it is.  You are trying to 
call newSubPol.GetItem, but wx.TextEntryDialog doesn't know what you are 
talking about so it complains.

Since you are wanting to make sure that you are adding the sub-item to 
an existing item, then you probably need to either check what the 
currently selected item in the tree is, or have already saved it 
somewhere.  Is that what self.polID is?


>   The second issue is how to gracefully handle the user clicking on the
> CANCEL key so that the dialog box is destroyed and I don't get a seg fault
> as a result of the actiion.

Just dedent the newSubPol.Destroy() line so it is always executed.  I 
doubt that would cause the segfault though, so that is probably 
something else.

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





More information about the wxpython-users mailing list