如何定期调用Python turtle 中的函数?

How to call periodically a function in Python turtle?

我必须用 turtle 库编写一个游戏。 在规则中,有一个物体每隔X毫秒就会倒下,直到达到某个条件。

如果我使用 sleep(),屏幕不再响应键盘事件。有什么方法可以定期调用函数 "asynchronously" 吗? 非常感谢!

Python turtle 提供了 one-shot:

screen.ontimer(my_function, milliseconds)

您可以将其变成每 X 毫秒 计时器事件:

def my_function():
    pass  # do whatever

    if still_needed:  # start again in the future if still needed
        screen.ontimer(my_function, 100)  # repeat every 0.1 second