Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我们应该在我们的实验室中使用函子,然后改用函数闭包。对于我们从 functools 导入 partial 作为命令的仿函数,设置最终看起来像:
digit = command(self.add_character, text = number) label.bind('< Button-1 >', digit)
然后我们将在其中将后来开发的方法绑定到标签...我将如何使用函数闭包而不是仿函数来做这样的事情?
您将使用 lambda 绑定匿名函数:
label.bind('< Button-1 >', lambda ev: self.add_character(ev, text=number))
在这里,我假设标签回调通常在事件参数中传递。您可能需要调整回调传递的参数数量。这是一个tkinter基于 - 的 GUI,只是要传入事件。
tkinter
该number值是封闭的,而在使用functools.partial()对象时,该number值将作为参数绑定到可调用对象。
number
functools.partial()