我正在尝试使用Tkinter将类方法绑定到信号,但是我收到以下错误:
TypeError:event_foo()只取1个参数(给定2个)
我过去曾经使用过很多绑定而没有任何问题,但我不明白第二个论点(我显然是在不知道的情况下提供)来自哪里。
代码示例:(简化)
class Controller:
def__init__(self, myVar, myWidget):
self.myVar = myVar
self.myWidget = myWidget
self.connect(self, myWidget, "<Double-Button-1>", event_foo)
def event_foo(self):
""" Does stuff """
#Simplified to a wrapper, real function does other actions
def connect(self, widget, signal, event)
widget.bind(signal, event)
答案 0 :(得分:4)
我在输入问题时想出来,所以我会回答自己以防其他人遇到此问题以下是修复示例的代码:
class Controller:
def__init__(self, myVar, myWidget):
self.myVar = myVar
self.myWidget = myWidget
self.connect(self, myWidget, "<Double-Button-1>", event_foo)
def event_foo(self, event=None): ###event was the implicit argument, I set it to None because I handle my own events and don't use the Tkinter events
""" Does stuff """
#Simplified to a wrapper, real function does other actions
def connect(self, widget, signal, event)
widget.bind(signal, event)
仍然,对如何运作的澄清将受到高度赞赏。它是可靠的还是我正在使用一种危险的丑陋工作,迟早会在我的脸上爆裂?
答案 1 :(得分:0)
我得到以下代码以相同的行为工作,但更简单。
class Controller:
def __init__(self, myVar, myWidget):
self.myVar = myVar
self.myWidget = myWidget
self.myWidget.bind('<Double-Button-1>', self.event_foo)
def event_foo(self, event):
""" Does stuff """