Python Turtle window crashes every 2nd time running

Question:

The code below is a basic square drawing using Turtle in python.

Running the code the first time works. But running the code again activates a Turtle window that is non-responsive and subsequently crashes every time.

The error message includes raise Terminator and Terminator

Restarting kernel in Spyder (Python 3.6 on a Dell Desktop) fixes the problem in that I can then run the code again successfully, but the root cause is a mystery?

Link to another question that is similar but as yet unanswered.

Please +1 this question if you find it worthy of an answer!!

import turtle
bob = turtle.Turtle()
print(bob)
for i in range(4):
    bob.fd(100)
    bob.lt(90)

turtle.mainloop()
Asked By: Daniel Moisio

||

Answers:

I realize this will seem wholly unsatisfactory, but I have found that creating the turtle with:

try:
    tess = turtle.Turtle()
except:
    tess = turtle.Turtle()  

works (that is, eliminates the “working every other time” piece. I also start with

wn = turtle.Screen()

and end with

from sys import platform
if platform=='win32':
    wn.exitonclick()

Without those parts, if I try to move the turtle graphics windows in Windows, things break. (running Spyder for Python 3.6 on a Windows machine)
edit: of course, OSX is perfectly happy without the exitonclick() command and unhappy with it so added platform specific version of ending “feature fix.” The try…except part is still needed for OSX.

Answered By: DukeEgr93

The module uses a class variable _RUNNING which remains true 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 example beyond what DukeEgr93 has proposed

1)

import importlib
import turtle

importlib.reload(turtle)

bob = turtle.Turtle()
print(bob)
for i in range(4):
    bob.fd(100)
    bob.lt(90)

turtle.mainloop()


import importlib
import turtle

turtle.TurtleScreen._RUNNING=True
bob = turtle.Turtle()
print(bob)
for i in range(4):
    bob.fd(100)
    bob.lt(90)

turtle.mainloop()


Answered By: William