【发布时间】:2019-10-04 05:26:57
【问题描述】:
我正在使用 Tkinter 每秒在画布中移动一个圆圈一定量,我在 Python 中遇到了 TypeError: 'NoneType' object is not callable 错误。我相信错误出在这段代码中:
def move(new_x0, new_y0, new_x1, new_y1):
new_x0 = new_x0 + speed
new_y0 = new_y0 + speed
new_x1 = new_x1 + speed
new_y1 = new_y1 + speed
game.canvas.delete("all")
obj = game.canvas.create_oval(new_x0, new_y0, new_x1, new_y1, fill = color)
game.canvas.pack()
t = threading.Timer(1.0, move(x0, y0, x1, y1))
t.start()
我希望画布上的圆圈在 1 秒后移动一次位置,但它只显示 NoneType 错误。
编辑:对不起,我忘了显示错误。在这里。
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\threading.py", line 1158, in run
self.function(*self.args, **self.kwargs)
TypeError: 'NoneType' object is not callable
编辑:我通过执行 return obj 解决了 NoneType 错误,然后我得到 int object is not callable,我通过执行 ShadowRanger 的建议解决了这个问题,所以我的代码现在可以工作了。
【问题讨论】:
-
哪个错误?不可迭代或不可调用?
-
显示完整的回溯
-
我不确定它是否需要参数,但我相信您打算将对
move的调用包装在lambda中。您将move的返回值传递给计时器,它不会返回任何内容。我不确定您是否想要x0等定期更新。 -
您的错误在这里:
t = threading.Timer(1.0, move(x0, y0, x1, y1))Timer的第二个参数必须是函数。您正在传递None(函数move没有return语句,因此它隐式返回None)。所以当它尝试调用该函数时,它最终会尝试调用None,这当然会产生错误。 -
@TomKarzes 我不确定如何解决这个问题。你能帮我写出代码吗?
标签: python python-3.x tkinter tkinter-canvas nonetype