首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python turtle.Terminator错误,有人能向我解释一下吗?

Python turtle.Terminator错误,有人能向我解释一下吗?
EN

Stack Overflow用户
提问于 2021-01-17 14:59:43
回答 2查看 251关注 0票数 0

我使用的代码来自极客健忘网站

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

当我关闭乌龟窗口时,我不得不在另一个问题上回答,但它仍然对我不起作用。

对错误的描述:

代码语言:javascript
运行
复制
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
EN

回答 2

Stack Overflow用户

发布于 2021-01-18 05:24:17

您引用的GeeksForGeeks示例实现得很糟糕。它使用的是while True:time.sleep(delay),它们在像海龟这样的事件驱动环境中没有任何位置。which提供了ontimer()事件来更好地处理这种情况,这将允许您干净地退出程序。

这里有一个SO example with the same issue,您应该能够通过搜索找到它。

票数 0
EN

Stack Overflow用户

发布于 2022-01-01 20:36:40

我同意GeeksForGeeks实现不是最优实现的观点。然而,考虑到它是一个学习网站,在您成为编写事件驱动代码的专家之前演示了一个基本的游戏化学习代码(甚至测试两者之间的差异),一个潜在的解决办法是使用while True:,您可以使用while turtle.TurtleScreen._RUNNING==True并将其余的循环例程封装在try以外或try以外。事件驱动是正确的方法。

代码语言:javascript
运行
复制
while turtle.TurtleScreen._RUNNING==True
    try:
        wn.update()
        ...
        ... 
        time.sleep(delay)
    except:
        pass #Or Exception handling

由于您已经要求解释,当您关闭窗口时,屏幕将不运行,这将使turtle.TurtleScreen._RUNNING标志变为假。在下一次更新期间,这会引发结束符错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65762189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档