From: Kevin A. <al...@se...> - 2007-06-24 05:21:59
|
On Jun 23, 2007, at 12:36 PM, John Henry wrote: > There are people at the Python newsgroup suggesting > that I should avoid using exec to accomplish this. > Here's what they suggested: > ... > function = mouseclick_factory(self, name) # as > before > method = new.instancemethod(function, self, > self.__class__) > setattr(self, function.name, method) ... You're basically doing what addMethod does, so after the setattr call, try self._addHandler(method) As mentioned before you're going to have to create the component after the event handler is created and bound or its bind events "magic" won't happen correctly. That means this bit in your factory needs to be moved out and after the _addHandler call. parent.components[name] = {'type':'Button', 'name':name, 'label':name, 'position':(5, 5+id_num*30), 'text':name} The way the PythonCard event binding works now is more efficient as I said but it is faster in part because of the caching of event handler names which is what the call to _addHandler is doing. You can see all this code in model.py. The handler name is added to a simple dictionary used for lookups by the findHandler method. In the old system, there was a lot of looping done at event dispatch time to find the right handler, back when everything was dynamic, but when I streamlined the code, I kept the old strategy, but simply moved it into a one-time event binding when a component is created. > Unfortunately, it doesn't work. PythonCard doesn't > calll the fuction created by the mouseclick_factory. > However, if I place the following statement right > after the setattr call, it works (proving that the > function actually gets created). > > self.on_Button1_mouseClick(event) > > Any idea why this doesn't work? > > Thanks, > > -- > John Henry ka |