Reputation: 144
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.
Upvotes: 2
Views: 727
Reputation: 486
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()
Upvotes: 0
Reputation: 41872
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()
Upvotes: 1
Reputation: 175
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.
Upvotes: 0