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?

Recommended Answers

All 4 Replies

You forgot the event parameter from the callback function.

Kert,
in Python 2.5 your script works fine.
Means... what? A bug, I guess.

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()

You can also try this:

    def incSpeed(self, event):
        print("Test", event.keysym)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.