I'm trying to bind a class method to a signal using Tkinter but I get the following error:
TypeError: event_foo() takes exactly 1 argument (2 given)
I used binding a lot in the past without any problems but I don't understand where the 2nd argument (that I'm apparently giving without knowing) comes from.
Code example: (simplified)
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)
I figured it out as I was typing the question so I'll answer myself in case someone else runs into this problem Here's the code that fixes the example:
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)
Still, clarifications on how this works would be highly appreciated. It is reliable or I'm using a dangerous ugly work-around that will burst in my face sooner or later?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With