我的代码
for i in list_tasks:
globals()['icon1%s' % i].bind(on_press=lambda x: **print(#here I want to print his name)**
例如globals()['icon1%s' % i]
是"icon1clock"我想在按下时打印他自己的名字(icon1clock)
棘手的部分是定义一个lambda表达式来包装另一个值,而不是名称。我不愿费心去尝试。编写一个函数,让为你创建回调;回调函数可以是变量的闭包。
def make_callback(name):
def _(x):
print(name)
return _
# *Not* icon1clock = ...
icons = {
'clock': ...,
}
for i in list_tasks:
icons[i].bind(on_press=make_callback(i))