From: John H. <ec...@ya...> - 2007-06-24 08:44:09
|
No dice. Here's what I have: def mouseclick_factory(parent, name): def function(self, event): print "You clicked '%s'." % name function.name = "on_%s_mouseClick" % name method = new.instancemethod(function, parent, parent.__class__) setattr(parent, function.name, method) parent._addHandler(method) parent.components[name] = {'type':'Button', 'name':name, 'label':name, 'position':(5, 5+int(name[-1:])*30), 'text':name} return function class Minimal(model.Background): def on_initialize(self, event): self.components['field1'] = {'type':'TextField','name':'field1','position':(5, 5),'size':(150, -1),'text':'Hello PythonCard'} mouseclick_factory(self, "Button1") if __name__ == '__main__': app = model.Application(Minimal, None, rsrc) app.MainLoop() When I click on Button1, nothing happens. --- Kevin Altis <al...@se...> wrote: > > 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 > > ------------------------------------------------------------------------- > This SF.net email is sponsored by DB2 Express > Download DB2 Express C - the FREE version of DB2 > express and take > control of your XML. No limits. Just data. Click to > get it now. > http://sourceforge.net/powerbar/db2/ > _______________________________________________ > Pythoncard-users mailing list > Pyt...@li... > https://lists.sourceforge.net/lists/listinfo/pythoncard-users > -- John Henry |