首页 > 解决方案 > Python 库 Turtle turtle.Terminator 错误,谁能给我解释一下:(

问题描述

我使用的代码来自 geekforgeeks 网站

https://www.geeksforgeeks.org/create-a-snake-game-using-turtle-in-python/

当我关闭龟窗时发生错误我必须在另一个问题中回答,但它仍然对我不起作用

错误描述:

Traceback (most recent call last):
      File "c:/Users/Zxed/Documents/1. DOCUMENT/Coding/Python/Game/snake.py", line 156, in <module>
        snake()
      File "c:/Users/Zxed/Documents/1. DOCUMENT/Coding/Python/Game/snake.py", line 88, in snake
        layar.update()
      File "C:\Users\Zxed\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 1303, in update
        t._update_data()
      File "C:\Users\Zxed\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 2646, in _update_data
        self.screen._incrementudc()
      File "C:\Users\Zxed\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 1292, in _incrementudc
        raise Terminator
    turtle.Terminator

标签: pythonturtle-graphicspython-turtle

解决方案


您引用的 GeeksForGeeks 示例实施得很差。它使用while True:并且time.sleep(delay)在像turtle这样的事件驱动环境中没有位置。Turtle 提供了ontimer()事件来更好地处理这种情况,这将允许您干净地退出程序。

这是一个具有相同问题的 SO 示例,您应该可以通过搜索找到该示例。


推荐阅读