Python turtle.Terminator error [duplicate]
Asked Answered
B

2

6

When i am using turtle module to draw a circle with this simple function:

def draw_shape(self):
    canvas = Screen()
    t = Turtle()
    t.circle(self.r)
    canvas.exitonclick()

For the first time when i call this function it opens a new window and draw a circle, i click on it to exit and when i try to again call this function from menu in console i got an error:

Original exception was:
Traceback (most recent call last):
  File "main.py", line 136, in <module>
    main()
  File "main.py", line 132, in main
    OPTIONS[user_input][1](shapes)
  File "main.py", line 48, in handle_sixth_menu_option
    t = Turtle()
  File "/usr/lib/python3.6/turtle.py", line 3816, in __init__
    visible=visible)
  File "/usr/lib/python3.6/turtle.py", line 2557, in __init__
    self._update()
  File "/usr/lib/python3.6/turtle.py", line 2660, in _update
    self._update_data()
  File "/usr/lib/python3.6/turtle.py", line 2646, in _update_data
    self.screen._incrementudc()
  File "/usr/lib/python3.6/turtle.py", line 1292, in _incrementudc
    raise Terminator
turtle.Terminator
Bayonet answered 17/10, 2017 at 18:27 Comment(1)
Is your goal to open a new window for every shape? That's a strange use case. Generally, turtle is used continually in one window, drawing multiple shapes. In the normal case, calling exitonclick, done or bye is only done at the very end of the whole app, and you don't call any further turtle methods. Python Turtle.Terminator even after using exitonclick() is the canonical for this use case. On the other hand, if you want to intentionally open and close multiple windows, this thread can address that use case.Saltus
G
3

This is because the turtle module (most reference implementations as of today) uses a class variable called _RUNNING. This becomes false during the exitonclick() method.

Changing your code to below should help.

import turtle

def draw_shape(self):
    canvas = Screen()
    turtle.TurtleScreen._RUNNING=True
    t = turtle.Turtle()
    t.circle(self.r)
    canvas.exitonclick()

Gielgud answered 1/1, 2022 at 18:54 Comment(1)
Pretty much the same answer as this one. Best to post in one place and mark a duplicate.Saltus
I
1

You can try the following:

def draw_shape(self): 
    import turtle as t
    canvas = Screen() 
    t.circle(self.r)                                                    
    canvas.exitonclick()

The reason your code wasn't working was because of the fact that you had already deleted or exited the instance of the turtle in the def function already once before by clicking on exit. Hence by using import turtle as t you are calling it again and creating a new instance.

Incoherent answered 9/12, 2017 at 6:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.