Python Turtle: how to resolve "turtle.Terminator" error?

Question:

I’m beginner to python turtle module and trying to create a program that will take input from user and draw a shape according to the inputs: here is the code,

import turtle
shapes = []
def draw_shape(sides, size, color):
    turtle.color(color)
    for i in range(sides):
        turtle.forward(size)
        turtle.left(360/sides)
while True:
    print("Options:")
    print("1. Draw the shape.")
    print("2. Print the names of all the shapes already drawn.")
    print("0. End the program.")
    choice = int(input("Enter your choice: "))
    if choice == 1:
        shape_name = input("Enter a name for the shape: ")
        shapes.append(shape_name)
        sides = int(input("Enter the number of sides: "))
        size = int(input("Enter the size of the shape: "))
        color = input("Enter the color of the shape: ")
        turtle.penup()
        turtle.goto(0, 0)
        turtle.pendown()
        draw_shape(sides, size, color)
        turtle.exitonclick()
    elif choice == 2:
        print("Shapes drawn:")
        for shape in shapes:
            print(shape)
    elif choice == 0:
        break
    else:
        print("Invalid Choice!")
turtle.mainloop()

the problem is when I run this loop more than 1 time it raises an error turtle.Terminator or sometimes graphics window goes for Not Responding, I can’t figure out the issue, anyone can help me with that?

Asked By: Shahid

||

Answers:

The reason the turtle window is not responding is that you have an input function that hasn’t been answered.
When there is an input function, the turtle will keep on waiting until the input has been answered.

Answered By: Hien Nguyen

Remove the turtle.exitonclick() line, this is what is causing the error.

I think that the problem is that the window is waiting for the user to click on the window to close it, which makes the code effectively stop at that line until the user clicks on the window. When the window is clicked, the window is then closed, causing the turtle to disappear, resulting in an error on the next loop since there is no longer a window to draw shapes in.

Answered By: meable

turtle.exitonclick() didn’t work for me, the solution is in turtle module there is a class instance _RUNNING in class TurtleScreen, that turns to false after drawing a shape, we can set this to true after every drawing, so the updated code for the draw_shape() will be:

def draw_shape(_sides, _size, _color):
    # Set the color of the turtle to the specified color
    turtle.color(_color)
    # Draw the shape with the specified number of sides and size
    for i in range(_sides):
        turtle.forward(_size)
        turtle.left(360/_sides)
    # Make the turtle stay on the screen until user closes the window
    turtle.exitonclick()

    # Reset the turtle screen for the next drawing
    turtle.TurtleScreen._RUNNING = True

Thanks who contributed to answer

Answered By: Shahid