wxPython: accessing wxFrame methods in __del__ fails

Alex VanderWoude nospam at biteme.com
Fri Mar 25 06:59:54 CET 2005

"Stephen Thorne" <stephen.thorne at gmail.com> wrote in message
news:mailman.802.1111645725.1799.python-list at python.org...
> On Thu, 24 Mar 2005 06:21:36 GMT, Alex VanderWoude <nospam at biteme.com>
> > I am attempting to save my window's size and position when it closes.
So I
> > figured I'd put some code in the __del__() method:
> >
> > from wxPython import *
> > class MyWindow(wxFrame):
> >     def __init__(self, parent, id=wxID_ANY, title=None, style=None):
> >         # Some stuff here.
> >     def __del__(self):
> >         x, y = self.GetPositionTuple()
> >         width, height = self.GetSizeTuple()
> >         # Store the values in a file or something.
> >         wxFrame.__del__(self)
> >
> > However, when I run this code I end up with an unhandled exception on
> > first line in __del__().  Apparently you can't call GetPositionTuple()
> > GetSizeTuple() for that matter) in __del__().  I'm confused by this,
> > I thought that __del__() was called before the object was actually
> > destroyed.  I can still do a dir(self), so obviously not everything
> > associated with my instance is gone.  Or does that work because it's
part of
> > the class, not just my instance?  Any insight into this would be greatly
> > appreciated.
> Don't use __del__, use EVT_CLOSE
> i.e.
> self.Bind(wx.EVT_CLOSE, self.OnClose)
> and
> def OnClose(self, evt):
> x, y = self.GetPositionTuple()
> --
> Stephen Thorne
> Development Engineer

Thanks for the tip.  There doesn't seem to be an equivalent EVT_OPEN, so I
guess I'll continue to use __init__.  That works fine.

One thing I noticed in my OnClose() event handler is that I must add a
Skip() command to pass the event futher up the chain, otherwise the app
doesn't actually close!  Furthermore, if an exception occurs during my
processing then the close is cancelled, so I also had to wrap my command in
a try block:

def OnClose(self, event):

- Alex

More information about the Python-list mailing list