> Hey people,
>
> I'm wondering about the best way to accomplish this.
>
> I have a GUI with two top-level windows, using a model-view-controller
> architecture. When the model changes, the views need to be notified and
> updated, and when the windows are closed, the model needs to know that
> the views are no longer open to be updated.
>
> Currently I'm using a simple list of object references, but I'm finding
> it problematic to determine when a window is no longer present.
>
> Any suggestions on how to do this with wx?
>
> Thanks,
> Mike
>
Maybe you could use wx.CallAfter.
Have your model:
1) keep a list of views (self._associated_views)
2) have a way to remove a view from the model -
def remove_view(self, view):
assert view in self._associated_views, "View not associated!"
del self._associated_views[self._associated_views.index(view)]
Have your view:
1) keep a handle to the model (self._model
2) use wx.CallAfter to tell the model to remove the current view when
the window is closed
wx.CallAfter(self._model.remove_view, self)
The syntax may be screwy somewhere, but the idea is sound (I think).
jw
I've used wx.lib.pubsub, wx.lib.evtmgr, and a roll your own approach
using weakrefs to solve this various times and all three work fine.
wx.lib.evtmgr is nice when you want your custom stuff to integrate
into the wx event framework, pubsub is nice for when you want it to
*not* do that, and the roll your own is, of course, nice for adding
your own precise semantics.
The key to all of them, and what makes doing MVC so much nicer in
Python than in C++, is weakrefs, which are a type of reference that
doesn't increase the reference count. A weakref can be notified when
its referant is released, and you can also safely dereference them
even if the referant is gone - you'll get None back, instead of an
error.
···
On 9/27/05, Jaime Wyant <programmer.py@gmail.com> wrote:
On 9/27/05, Michael P. Soulier <msoulier@digitaltorque.ca> wrote:
---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org