1

I just wrote a program based on turtle module and after last line of code I used turtle.done() but when again I want to use my turtle I will get Terminator error and I should close and again open jupyter or even commond line to be able to run my code again please tell me what's wrong? Is it a bug inside turtle? my code is something like this:

my_screen = turtle.Screen()
my_turtle= turtle.Turtle()
wm.setup(200, 300)

....
Here is my code(quite long!)
....

turtle.done()

ggorlen
  • 44,755
  • 7
  • 76
  • 106
WorldLover
  • 131
  • 1
  • 7
  • Please share a [mcve]. [Python Turtle.Terminator even after using exitonclick()](https://stackoverflow.com/questions/45534458/python-turtle-terminator-even-after-using-exitonclick) explains the likely error. – ggorlen Sep 21 '22 at 02:57

2 Answers2

0

The error occurs when a turtle program is interrupted during its main loop. If the way you close your turtle program is by clicking the X and there's a while loop that never ends, then the turtle.done() statement will never be met.

You can detect a turtle.Terminator error inside your while loop, so that instead of crashing the program, let the program continue to parse outside the while loop.

Something like this:

my_screen = turtle.Screen()
my_turtle= turtle.Turtle()
wm.setup(200, 300)
while True:
    try:
        # Your code for the while loop
    except turtle.Terminator:
        break
turtle.done

Or use ontimer().

Red
  • 26,798
  • 7
  • 36
  • 58
  • `turtle.done` here is a no-op without parentheses. Better to get to the root of the problem than `except` it and pretend the error didn't happen. Generally, terminator comes from terminating the turtle window and then trying to call more turtle methods. – ggorlen Sep 21 '22 at 02:54
0

The turtle module uses a class variable _RUNNING which remains true between executions when running in jupyter instead of running it as a self contained script. I have requested for the module to be updated with a pull request.

Meanwhile, work around/working example

1)

import importlib
import turtle
importlib.reload(turtle)
my_screen = turtle.Screen()
my_turtle= turtle.Turtle()
wm.setup(200, 300)

....
Your code
....

turtle.done()




import turtle
my_screen = turtle.Screen()
turtle.TurtleScreen._RUNNING=True
my_turtle= turtle.Turtle()
wm.setup(200, 300)

....
Your code
....

turtle.done()

William
  • 464
  • 2
  • 16