Please Hlp with msg: "The C++ part of the StaticText object has been deleted"
Cliff Wells
clifford.wells at comcast.net
Mon Sep 27 18:49:36 EDT 2004
On Sun, 2004-09-26 at 06:26 +0000, python newbie wrote:
> Anyway, when I click on a node of this tree, I change the text of the
> static text control at top, to the text of the node. It actually works,
> but when I close the app I get this:
> ----------------------------------------------
> The stack trace:
> E:\MyProjects1\python\backup
> Traceback (most recent call last):
> File "E:\MyProjects1\python\backup\wxProject.py", line 294, in
> OnNodeChanged
> self.testLab.SetLabel('test')
> File "C:\Python23\Lib\site-packages\wx\_core.py", line 10617, in
> __getattr__
> raise PyDeadObjectError(self.attrStr % self._name)
> wx._core.PyDeadObjectError: The C++ part of the StaticText object has been
> deleted, attribute access no longer allowed.
> ---------------------------------------------
>
> Here's how I add the wxStaticBox: ------------------------
> # ok, first, the code below is actually in this __init__ function
> class main_window(wxFrame):
> def __init__(self, parent, id, title):
> wxFrame.__init__(self, parent, -1, title, size = (650, 500),
> style=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE)
>
> # first the left panel
> # -------------------
> self.MyLeftPanel = wxPanel(splitter,10004) #<--a random number I used
Why a "random" number? Just use -1 or wxNewId() (and those are
equivalent, FWIW) if you don't care about the id. Your approach is
inherently unsafe, although I doubt it's the source of your problem.
> # didn't want to use -1
> self.MyLeftPanel.SetDimensions(0, 0, 100, 220)
> self.leftSizer=wxBoxSizer(wxVERTICAL) # the sizer is born
> self.MyLeftPanel.SetSizer(self.leftSizer)
> self.MyLeftPanel.SetAutoLayout(true)
>
> # then add the static text
> # -------------------
> self.aNewID=wxNewId()
> self.testLabel = wxStaticText(self.MyLeftPanel,self.aNewID,'test label')
> self.leftSizer.Add(self.testLabel)
>
>
> Here's how I code the event handler: ------------------
>
> def OnNodeChanged(self,event):
> item = self.tree.GetSelection()
> if (self.tree.ItemHasChildren(item) == 0):
> #self.testLabel.SetLabel(self.tree.GetItemText(item))
> self.testLabel.SetLabel('test')
> # The commented-out line works too.. the static text does change
> # to the static of the node --- very nice, if it weren't
> # for the error when you close the app.
>
> Any suggestions would be great.
I'm not sure where your error comes from (aside from obviously being the
result of accessing a destroyed widget), but this will prevent the error
message (a band-aid, more or less):
if self.testLabel:
self.testLabel.SetLabel('test')
I'm guessing that OnNodeChanged gets called when the tree is destroyed,
which may be after the StaticText is destroyed, which could be your
problem (in which case the fix I provided is fine). I can't be certain
of this since I have no idea what event triggers your event handler (but
if it's EVT_TREE_DELETE_ITEM then my guess is probably correct.
Regards,
Cliff
--
Cliff Wells <clifford.wells at comcast.net>
More information about the Python-list
mailing list