From: Thomas H. <th...@py...> - 2003-08-13 08:52:33
|
"Mike C. Fletcher" <mcf...@ro...> writes: > Thomas Heller wrote: > ... > >>I hope this forum is the correct one to ask questions about Patrick's >>dispatcher module ;-) >> > Yup, this is the place. Though I'm rather busy these days, so there > may be considerable delays in response from me particularly. > >>The problem occurs when the event handling become more complicated, and >>cannot simply be done by a simple (bound) method call in a widget >>instance. >> >>What I want to achive is something like this: >> >> def on_create(self, widget): >> name = widget.label >> import registry >> model = registry.get(name) >> def handler(model, widget=widget): >> data = model.get_some_data() >> widget.show_value(data) >> >> dispatcher.connect(signal=(name, "changed"), >> receiver=handler) >> >>In other words, the handler for the event must do an operation on the >>model for some data, and then call a method on the widget. (The >>'changed' event in the above code has the model as a parameter to make >>this possible). >> > If I understand correctly, what you want to do is to have the handler > function live a life of its own (not be collected) until the widget > goes out of scope, at which point you want to drop the handler > function. > > class Holder( object ): > def __init__( self, method, client ): > self.method= method # strong reference, kept alive > self.client = saferef.safeRef( client, onDelete = self.cleanup ) > def __call__( self, *args, **named ): > if self.client: > client = self.client() > if client is not None: > return self.method( *args, **named ) > def cleanup( self, oldWeakRef ): > self.client = None > dispatcher.disconnect( self ) > > I haven't tested that, but it should outline the basic approach. You > use weak=False when registering the Holder instance, and the holder > takes care of managing the lifetime of the method (keeps a strong ref > until the client widget goes away). Mike, thanks for this suggestion. It didn't work exactly as shown above, but it showed me the way to do it. I assume you meant self.client = weakref.ref(client, self.cleanup) instead of self.client = saferef.safeRef( client, onDelete = self.cleanup ) or did I miss something. And sorry for the late reply. Thomas |