令人困惑的类型错误 [英] Confusing TypeError
本文介绍了令人困惑的类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个小的 Python 程序,它应该通过运行适当的方法来响应按下向上按钮.但不是这样做,它给了我一个令人困惑的错误......
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()
错误是:
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)
可能是什么问题?
推荐答案
incSpeed
方法应该接受一个额外的参数;你的只需要 self
但它传递了一个 事件论据也是如此.
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")
这篇关于令人困惑的类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文