Confusing TypeError

别说谁变了你拦得住时间么 提交于 2019-12-10 18:44:46

问题


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?


回答1:


The incSpeed method should take an extra argument; yours only takes self but it is passed an event argument as well.

Update your function signature to accept it:

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


来源:https://stackoverflow.com/questions/12507278/confusing-typeerror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!