0

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?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • Does this answer your question? [Python Turtle.Terminator even after using exitonclick()](https://stackoverflow.com/questions/45534458/python-turtle-terminator-even-after-using-exitonclick) – ggorlen Jan 25 '23 at 15:26
  • @ggorlen no, but I figured out the solution –  Apr 07 '23 at 05:20
  • Great to hear! Could you post a [self answer](https://stackoverflow.com/help/self-answer) to explain what you did to solve it? This helps other people with the same problem. Thanks. That said, I'll be a bit surprised if it's anything different than the link, which says as much as the answers here do: remove the extra exit. Once you exit turtle, that's it--you can't call any further turtle functions (unless you re-enable turtle by invading a private variable, also shown in the other thread). – ggorlen Apr 07 '23 at 05:22
  • @ggorlen yes, maybe any other thread could answer my question because re-enabling the private variable was the solution, but I didn't find that thread, so just posted the answer here. –  Apr 07 '23 at 05:36
  • Well, that answer is [in the other thread](https://stackoverflow.com/a/50725605/6243352), so the dupe suggestion seems correct. It's best for future visitors if all the knowledge is consolidated into one thread rather than reinventing all of it here. Appreciate you following up, though. As I commented in that thread, I don't think this is a good solution since it relies on an internal, undocumented variable in the turtle module that could change at any future release. Better to use a click listener rather than abusing `exitonclick` as such. – ggorlen Apr 07 '23 at 05:39
  • Thanks for the thread link, I understand that is not a good solution to change any private variable but as the main loop is set to `while True`, using `exitonclick()` stop the screen until user click to exit, in this way loop is not going for next iteration until window exits, another possible solution might be using `multithreading` so turtle and while loop uses different threads to work in background as well, let me know if this could be a solution. –  Apr 07 '23 at 06:02

0 Answers0