Intercept the Destroy method.

Hi,

I’m new to wxPython. The application I’m writing uses an MVC architecture. There is a Status_model that contains state information about the application. Then, there can be multiple Status_views that display this information. The Status_views register themselves with the model by calling an add_listener method. The Status_view is derived from wx.PyPanel. I’ve attached a stripped down Python outline.

The problem I’m running into is when the Status_view is destroyed, I’d like it to unregister itself by calling a remove_listener method of the Status_model. Otherwise when the Status_model changes it will try to update the destroyed/dead Status_view and and a PyDeadObjectError exceptions is raised.

I looked in the wxWidgets documentation. I see the wxWindow::Destroy method is virtual. so, I thought I could just implement a Destroy method in my Status_view class that calls the remove_listener method. But, that destroy method is never called. Surely, there is someway for a Window to find out that it is about to be destroyed so it can tie up some loose ends. I suspect that overriding the wxWindow::Destoy method is the way to do this in C++ applications. (Please correct me if this is wrong) But, I can’t figure out how to do this in Python. I’d appreciate any suggestions about a better way to do this.

Thanks,

Jeff

xxxx.py (1.19 KB)

Hi,

···

On Fri, Aug 31, 2012 at 3:50 PM, Jeff Haper <jeff1.61803@gmail.com> wrote:

I looked in the wxWidgets documentation. I see the wxWindow::Destroy method
is virtual. so, I thought I could just implement a Destroy method in my
Status_view class that calls the remove_listener method. But, that destroy
method is never called. Surely, there is someway for a Window to find out
that it is about to be destroyed so it can tie up some loose ends. I
suspect that overriding the wxWindow::Destoy method is the way to do this in
C++ applications. (Please correct me if this is wrong) But, I can't figure
out how to do this in Python. I'd appreciate any suggestions about a better
way to do this.

It may be Virtual in C++ but not all of the virtual methods are
overridable in wxPython.

Bind to EVT_WINDOW_DESTROY the event handler bound to this event is
called when a window is Destroyed.

Cody

Hi,

I looked in the wxWidgets documentation. I see the wxWindow::Destroy method
is virtual. so, I thought I could just implement a Destroy method in my
Status_view class that calls the remove_listener method. But, that destroy
method is never called. Surely, there is someway for a Window to find out
that it is about to be destroyed so it can tie up some loose ends. I
suspect that overriding the wxWindow::Destoy method is the way to do this in
C++ applications. (Please correct me if this is wrong) But, I can't figure
out how to do this in Python. I'd appreciate any suggestions about a better
way to do this.

It may be Virtual in C++ but not all of the virtual methods are
overridable in wxPython.

http://wiki.wxpython.org/OverridingMethods

Bind to EVT_WINDOW_DESTROY the event handler bound to this event is
called when a window is Destroyed.

Be careful with it though. IIRC the window is in the process of being destroyed at the time the event is sent, so calling methods of that window could cause problems.

···

On 8/31/12 1:56 PM, Cody wrote:

On Fri, Aug 31, 2012 at 3:50 PM, Jeff Haper <jeff1.61803@gmail.com> wrote:

--
Robin Dunn
Software Craftsman