令人困惑的TypeError

use*_*623 3 python tkinter self typeerror

我有一个小的Python程序,它应该通过运行适当的方法来响应按下向上按钮.但它没有这样做,它给了我一个令人困惑的错误......

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()
Run Code Online (Sandbox Code Playgroud)

错误是:

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)
Run Code Online (Sandbox Code Playgroud)

可能是什么问题?

Mar*_*ers 5

incSpeed方法应该采取额外的论点; 你的只需要self它,但它也传递了一个事件参数.

更新您的功能签名以接受它:

def incSpeed(self, event):
    print("Test")
Run Code Online (Sandbox Code Playgroud)