I have a small Python program that should react to pushing the up button by running an appropriate method. But instead of doing this, it gives me a confusing error...

from tkinter import *

class App:

    def __init__(self, master):

        self.left = 0
        self.right = 0
        widget = Label(master, text='Hello bind world')
        widget.config(bg='red')            
        widget.config(height=5, width=20)                  
        widget.pack(expand=YES, fill=BOTH)

        widget.bind('<Up>',self.incSpeed)   
        widget.focus()

    def incSpeed(self):
        print("Test")

root = Tk() 
app = App(root)
root.mainloop()

And the error is:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.2/tkinter/__init__.py", line 1402, in __call__
    return self.func(*args)
TypeError: incSpeed() takes exactly 1 positional argument (2 given)

What might be the problem?

Tkinter bind passes an event argument that you have to take care of even if you don't use it:

from tkinter import *

class App:

    def __init__(self, master):

        self.left = 0
        self.right = 0
        widget = Label(master, text='Hello bind world')
        widget.config(bg='red')
        widget.config(height=5, width=20)
        widget.pack(expand=YES, fill=BOTH)

        widget.bind('<Up>',self.incSpeed)
        widget.focus()

    def incSpeed(self, event):
        print("Test")

root = Tk()
app = App(root)
root.mainloop()

Edited 4 Years Ago by ZZucker

You can also try this:

    def incSpeed(self, event):
        print("Test", event.keysym)
This question has already been answered. Start a new discussion instead.