令人困惑的类型错误

2024-04-14

我有一个小型 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()

错误是:

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)

可能是什么问题?


The incSpeed方法应该带有一个额外的参数;你的只需要self但它通过了事件论证 http://docs.python.org/library/tkinter.html#bindings-and-events以及。

更新您的函数签名以接受它:

def incSpeed(self, event):
    print("Test")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

令人困惑的类型错误 的相关文章