简体   繁体   中英

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

Here is my code:

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()

Whenever I run this program, the first time it comes up with this error (Error below). However, the second time I run it, it runs fine. Error:

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

The error comes from here in 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

I don't want to have to run this code twice every time so if anyone has a solution, I thank you in advance.

Don't call both:

sc.exitonclick()
mainloop()

It's one or the other as exitonclick() simply sets up the exit key event and calls mainloop()

TurtleScreen._RUNNING is False when the program is executed for the first time, so it will enter the judgment formula and throw a Terminator exception

TurtleScreen._RUNNING is True when the program is executed for the second time, skipping the judgment formula, so it executes smoothly.

Delete raise Terminator , you can solve the problem.

I can see from your filepath that you are running it from spyder. The turtle module uses a class variable TurtleScreen._RUNNING which remains false after a destroy between executions when running in spyder instead of running it as a self contained script. I have requested for the module to be updated.

Meanwhile, work around/working examples

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM