From: Mike C. F. <mcf...@ro...> - 2003-07-31 23:46:27
|
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). >Any ideas, anyone? Is this a common problem? > >Thomas > > Don't know how common it is, I've seen similar patterns in a few cases, but I do a lot of meta-programming. HTH, Mike _______________________________________ Mike C. Fletcher Designer, VR Plumber, Coder http://members.rogers.com/mcfletch/ |