当我 运行 这个程序时,为什么会出现终止符错误?

Why do I get a Terminator error when I run this program?

这是我的代码:

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

我不想每次都要 运行 这段代码两次,所以如果有人有解决方案,我提前谢谢你。

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

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

删除raise Terminator,即可解决问题

不要同时调用:

sc.exitonclick()
mainloop()

这是一个或另一个,因为 exitonclick() 只需设置退出键事件并调用 mainloop()

我从你的文件路径可以看出你是 运行 它来自 spyder。 turtle 模块使用 class 变量 TurtleScreen._RUNNING,当在 spyder 中 运行 而不是 运行 时,它在两次执行之间的销毁后仍然为 false 作为自包含脚本。我已请求更新模块。

与此同时,工作 around/working 个例子

1)

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

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