微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么我在运行这个程序时会收到 Terminator 错误?

如何解决为什么我在运行这个程序时会收到 Terminator 错误?

这是我的代码

import os
from turtle import *
sc = Screen()
sc.setup(600,600)
image = os.path.expanduser("~\OneDrive\Desktop\AlienGameImage.gif")
sc.register_shape(image)
t = Turtle()
t.shape(image)
sc.exitonclick()

每当我运行这个程序时,它第一次出现这个错误(下面的错误)。但是,我第二次运行它时,它运行良好。错误

Traceback (most recent call last):

  File "C:\Users\hulks\.spyder-py3\untitled0.py",line 14,in <module>
    t = Turtle()

  File "C:\Users\hulks\anaconda3\lib\turtle.py",line 3813,in __init__
    RawTurtle.__init__(self,Turtle._screen,File "C:\Users\hulks\anaconda3\lib\turtle.py",line 2557,in __init__
    self._update()

  File "C:\Users\hulks\anaconda3\lib\turtle.py",line 2660,in _update
    self._update_data()

  File "C:\Users\hulks\anaconda3\lib\turtle.py",line 2646,in _update_data
    self.screen._incrementudc()

  File "C:\Users\hulks\anaconda3\lib\turtle.py",line 1292,in _incrementudc
    raise Terminator

Terminator

错误来自于turtle.py:

def _incrementudc(self):
    """Increment update counter."""
    if not TurtleScreen._RUNNING:
        TurtleScreen._RUNNING = True
        raise Terminator
    if self._tracing > 0:
        self._updatecounter += 1
        self._updatecounter %= self._tracing

我不想每次都运行两次此代码,所以如果有人有解决方案,我提前感谢您。

解决方法

不要同时调用:

sc.exitonclick()
mainloop()

它是其中之一,因为 exitonclick() 只是设置退出键事件并调用 mainloop()

,

TurtleScreen._RUNNING是程序第一次执行时的False,所以会进入判断公式,抛出Terminator异常

TurtleScreen._RUNNING为程序第二次执行时的True,跳过判断公式,所以执行顺利。

删除raise Terminator,即可解决问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。