【发布时间】:2020-02-14 20:20:21
【问题描述】:
我正在使用 tkinter 学习,这涉及到形状的流畅运动。这就是我现在卡住的地方。
我得到不一致的递归错误。该代码在 Windows 1O 上运行没有问题,但在我的 macOS 上它崩溃了。
此外,如果递归之间的时间≥ 17 毫秒,它就会停止出错并按预期运行。我已经简化了代码,只是为了解决一个问题。
from tkinter import *
master = Tk()
canvas = Canvas(bg = "gray", width = 1000, height = 800)
canvas.pack()
cara = canvas.create_line(100,100,900,100, width = 5, fill = "red")
def moveCara():
canvas.move(cara, 0,1)
canvas.after(16, moveCara) # Here is the time setting, change it to 17 and the thing
# does not crash.
canvas.update()
moveCara()
mainloop()
此代码导致我的 MacBook 崩溃:
RecursionError: maximum recursion depth exceeded during compilation
但是,如果我将 canvas.after() 中的时间更改为 17 或更大,一切正常。这是为什么呢?
【问题讨论】:
-
尽量不要打电话给
canvas.update() -
另外,取决于你想做什么。
pygame将是一个很好的库。 -
尝试调用
canvas.update_idletasks()而不是canvas.update()。后者“可能导致不可预测的行为或循环”。见Universal widget methods。