wxPython: accessing wxFrame methods in __del__ fails

A

Alex VanderWoude

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 the
first line in __del__(). Apparently you can't call GetPositionTuple() (or
GetSizeTuple() for that matter) in __del__(). I'm confused by this, because
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.

- Alex
 
S

Stephen Thorne

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 the
first line in __del__(). Apparently you can't call GetPositionTuple() (or
GetSizeTuple() for that matter) in __del__(). I'm confused by this, because
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()
 
A

Alex VanderWoude

Stephen Thorne said:
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()

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):
try:
self.SaveFrameSettings()
finally:
event.Skip()

- Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top