From: Kevin A. <al...@se...> - 2007-06-24 17:20:33
|
After a bit of research I came up with the following working example which uses a slightly modified version of your original factory. func_name seems to be the important attribute. I was able to use addMethod to clean up the code a bit . This still doesn't solve the problem of creating an arbitrary function/method from a string, but that's a generic Python problem that I don't know the answer to rather than something PythonCard- specific. ka --- from PythonCard import model rsrc = {'application':{'type':'Application', 'name':'Minimal', 'backgrounds': [ {'type':'Background', 'name':'bgMin', 'title':'Minimal PythonCard Application', 'size':(200, 100), 'components': [ ] # end components } # end background ] # end backgrounds } } 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'} self.mouseclick_factory("Button1") self.mouseclick_factory("Button2") def mouseclick_factory(self, name): def function(self, event): # changed to event.target.name to verify we're getting # the correct target when button is clicked print "You clicked '%s'." % event.target.name # func_name seems to be the magic attribute rather than # just setting func.name function.func_name = "on_%s_mouseClick" % name self.addMethod(function) self.components[name] = {'type':'Button', 'name':name, 'label':name, 'position':(5,5+int(name[-1:]) *30), 'text':name} return function if __name__ == '__main__': app = model.Application(Minimal, None, rsrc) app.MainLoop() |