<分区>
我目前正在尝试创建一个循环,将事件 Action 对绑定(bind)到字典中。 回调函数只是调用 Action 函数并打印参数。
for binding, action in self.keyMap.revMap.items() :
print binding, action
self.top.bind(binding,
lambda event : self.callback(event, self.actionRep.__class__.__dict__[action]))
print self.top.bind()
在绑定(bind)时,我得到这些日志:
<Escape> toggleMenu -------------generated by the line "print binding, action"
<Return> toggleChat -------------generated by the line "print binding, action"
('<Key-Return>', '<Key-Escape>', '<Key>') ---generated by the line "print self.top.bind()"
事件- Action 对是正确的。 但是,当事件发生时,我有这个:
<Tkinter.Event instance at 0x0000000002BB3988> <function toggleChat at 0x0000000002B60AC8>
<Tkinter.Event instance at 0x0000000002BB3948> <function toggleChat at 0x0000000002B60AC8>
也就是说,escape 和 return 事件似乎都绑定(bind)到 toggleChat...
我对 lambda 表达式没有什么经验,但我希望为每个循环创建一个新的无名函数。我错了吗?如果不是,问题出在哪里?
提前感谢您的见解。