0

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?

4
Contributors
4
Replies
6
Views
4 Years
Discussion Span
Last Post by ZZucker
Featured Replies
  • 1

    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): … Read More

  • 1

    You can also try this: def incSpeed(self, event): print("Test", event.keysym) Read More

1

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 by ZZucker

1

You can also try this:

    def incSpeed(self, event):
        print("Test", event.keysym)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.