I am having trouble with running this code. I am trying to run an instance of turtle, successfully close it, and start a new instance of the turtle. I am able to complete the first iteration but I get a terminator exception raised: Exception has occurred: Terminator exception: no description
at self.turt = Turtle()
, when I debug the program.
I know self.screen.exitonclick()
disposes the turtle and screen, but I thought I was creating a new instance of turtle and screen when I would call the class in Call_Turtle_Looping()
below.
import numpy as np
from turtle import Turtle,Screen
class Turtle_Looping():
def __init__(self):
self.turt = Turtle()
self.screen = Screen()
self.angles = [20,45,65,80,90,95]
self.colors = ["red","blue","red","black","red","blue"]
def Basic_Move(self):
for i in range(len(self.angles)):
self.screen.tracer(0)
self.turt.pendown()
self.turt.color(self.colors[i])
self.turt.right(self.angles[i])
self.turt.forward(150)
self.turt.penup()
self.screen.update()
self.screen.exitonclick()
def Call_Turtle_Looping():
for i in range(4):
print(f"we are on run #{i}")
inst = Turtle_Looping()
inst.Basic_Move()
Call_Turtle_Looping()
The following solution is a hack and is intended for use only in simple programs. It may not follow best practices and might lead to unexpected behavior in more complex or production-level code. Use it with caution.
When screen.exitonclick()
is called, a Turtle module global variable _RUNNING
is set to False
. To fix this, we need to rearrange your __init__
function and set it to True
:
from turtle import Turtle, Screen, TurtleScreen
class Turtle_Looping():
def __init__(self):
self.screen = Screen()
TurtleScreen._RUNNING=True #This has to happen before you call Turtle()
self.turt = Turtle()
That's it!
Alternative: self.screen.onclick(lambda a,b: self.screen.reset())
Resets the Screen
on click.