Fabian,
I think this code should be fine, though I wouldn't use the Bind/Unbind code (too much overhead, could potentially trigger bugs in wx ;). Better set a status variable like this:
def update(self, subject):
self.in_update = True
self._organizer_text_ctrl.SetValue(subject.organizer)
self.in_update = False
def on_organizer_text_evt(self, event):
if in_update:
return
self._rm.organizer = event.GetEventObject().GetValue()
Markus
Fabian Sturm schrieb:
···
Hi!
I have a technical / philosophical question. I am writing a wxpython
application and I try to strictly separate the model from the view to
stay gui widget independant. (to be able to switch in the future to pure
gtk or something else if necessary).Therefore I use the observer pattern to distribute any changes in the
model to the widgets displaying the information.My question now is, does this make sense or are there any better
approaches. Sure I know that everything that works is okay, but is it
good programming practice? Mostly I am concerned about wxpython event
proagation, which I have to disable and enable all the time with a
Unbind/bind call. (That's because otherwise I end up in an infinite
event loop)Here my code so far:
class Model(object):
_organizer = ''
def __init__(self):
self._observers =
def attach(self, observer):
if not observer in self._observers:
self._observers.append(observer)def detach(self, observer):
try:
self._observers.remove(observer)
except ValueError:
passdef notify(self, modifier=None):
for observer in self._observers:
if modifier != observer:
observer.update(self)
def _get_organizer(self): return self._organizer
def _set_organizer(self, value): if value != self._organizer: self._organizer = value # propagate the change
self.notify()
organizer = property(_get_organizer, _set_organizer)class Ui(object):
def __init__(self, parent, res, Model):
self._rm = Model
self._rm.attach(self);
self._panel = res.LoadPanel(parent, "RegattaPanel")
self._organizer_text_ctrl = wx.xrc.XRCCTRL(self._panel,
"organizer_text_ctrl")
self._organizer_text_ctrl.Bind(wx.EVT_TEXT,
self.on_organizer_text_evt)
def update(self, subject):
self._organizer_text_ctrl.Unbind(wx.EVT_TEXT)
self._organizer_text_ctrl.SetValue(subject.organizer)
self._organizer_text_ctrl.Bind(wx.EVT_TEXT,
self.on_organizer_text_evt)
def on_organizer_text_evt(self,
event): self._rm.organizer = event.GetEventObject().GetValue()Thanks a lot for any comments regarding this code, especially concerning
the style! Fabian---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org