|
From: John H. <ec...@ya...> - 2007-06-23 19:36:40
|
There are people at the Python newsgroup suggesting
that I should avoid using exec to accomplish this.
Here's what they suggested:
#!/usr/bin/python
"""
__version__ = "$Revision: 1.6 $"
__date__ = "$Date: 2004/08/17 19:46:06 $"
"""
import new
from PythonCard import model
rsrc = {'application':{'type':'Application',
'name':'Minimal',
'backgrounds': [
{'type':'Background',
'name':'bgMin',
'title':'Minimal PythonCard Application',
'size':(200, 300),
'components': [
] # end components
} # end background
] # end backgrounds
} }
def mouseclick_factory(parent, name):
id_num=int(name[-1:])
parent.components[name] = {'type':'Button',
'name':name,
'label':name,
'position':(5,
5+id_num*30),
'text':name}
def function(self, event):
print "You clicked '%s'." % name
function.name = "on_%s_mouseClick" % 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'}
name = "Button1"
function = mouseclick_factory(self, name) # as
before
method = new.instancemethod(function, self,
self.__class__)
setattr(self, function.name, method)
if __name__ == '__main__':
app = model.Application(Minimal, None, rsrc)
app.MainLoop()
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
|