Python tkinter绑定具有多个参数的函数
我有一个一般性的问题,我真的找不到答案,希望你们能帮忙。我有一个带3个参数的函数,下面是一个例子Python tkinter绑定具有多个参数的函数,python,tkinter,arguments,Python,Tkinter,Arguments,我有一个一般性的问题,我真的找不到答案,希望你们能帮忙。我有一个带3个参数的函数,下面是一个例子 def someFunction(self, event, string): do stuff .. self.canvas.bind("<Button-1>", self.someFunction("Hello")) def someFunction(self、event、string): 做事。。 self.canvas.bind(“,self.someFunction(
def someFunction(self, event, string):
do stuff ..
self.canvas.bind("<Button-1>", self.someFunction("Hello"))
def someFunction(self、event、string):
做事。。
self.canvas.bind(“,self.someFunction(“Hello”))
当我运行这个程序时,我得到一个错误,说我传递了someFunction 2参数,而不是3参数。我不知道为什么…这里您正在绑定
someFunction
的结果。这会失败,因为当python尝试获取someFunction
的结果时,当someFunction
确实需要两个显式参数时,它只通过一个参数(“Hello”
)调用它。您可能需要以下内容:
self.canvas.bind('<Button-1>',lambda event: self.someFunction(event,"Hello"))
self.canvas.bind(“”,lambda事件:self.someFunction(事件,“Hello”))
这将绑定一个新函数(由lambda
创建并围绕self.someFunction
)传递正确的参数。或
def someFunction(self, string):
def fn(*arg)
print string
return fn
self.canvas.bind("<Button-1>",self.someFunction("Hello!"))
def someFunction(self,string):
def fn(*arg)
打印字符串
返回fn
self.canvas.bind(“,self.someFunction(“Hello!”))