1
我尝试用变量名定义一个函数。eval中的函数定义
名称从数据库中检索。对于每一个名字,我希望定义一个按钮,并具有独立的处理:
title=['BNL','CE']
for i in range(0,len(title)):
panelvpu.add(Button(title[i]))
for i in range(0,len(title)):
eval('def onButtonClick'+title[i]+'(self, event):')
eval(' Window.alert("Yes")')
按钮定义是好的,但在定义函数的事件的处理提供了一个错误
im1 SyntaxError: at index 4 in "def onMenu1Item1(self):
Window.alert("Item 1 selected")": expected ';', got 'onMenu1Item1'
反馈后,我改为
title=['BNL','CE']
for t in title : panelvpu.add(Button(t))
for t in title:
def_code = "print t"
exec(def_code)
只是为了得到感觉;在python下这工作正常。 但是我用睡衣和最后一个代码执行赖斯的错误,说明
im1 TypeError: iter is undefined
看来,睡衣是不支持的eval()和exec()呢。
理查德
使用'exec',但不要 – JBernardo
这对我来说看起来不像是单独的处理... –
看起来您正试图为回调命令创建一个功能工厂;好的想法,但是Python有更好的工具可以让你做到这一点。 – Thomas