By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,626 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,626 IT Pros & Developers. It's quick & easy.

wxPython: accessing wxFrame methods in __del__ fails

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Thu, 24 Mar 2005 06:21:36 GMT, Alex VanderWoude <no****@biteme.com> wrote:
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()

--
Stephen Thorne
Development Engineer
Jul 18 '05 #2

P: n/a
"Stephen Thorne" <st************@gmail.com> wrote in message
news:ma*************************************@pytho n.org...
On Thu, 24 Mar 2005 06:21:36 GMT, Alex VanderWoude <no****@biteme.com>

wrote:
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()

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

- Alex

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.