Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding class method to a Tkinter signal

Tags:

python

tkinter

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)
like image 822
Asics Avatar asked Jul 31 '13 19:07

Asics


1 Answers

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?

like image 143
Asics Avatar answered Sep 21 '22 13:09

Asics