2

I am using Python Turtle-graphics to draw some figures. However, I am not able to create it again once screen has been closed.

For example, following simple code works:

import turtle
t = turtle.Turtle()
t.speed(0)
t.dot()
t.goto(100, 100)
turtle.done()

However, in following code, error occurs at time of second drawing:

import turtle

# FIRST DRAWING: 
t = turtle.Turtle()
t.speed(0)
t.dot()
t.goto(100, 100)
turtle.done()

# SECOND DRAWING: 
t = turtle.Turtle()        # ERROR OCCURS HERE WHEN FIRST WINDOW IS CLOSED.
t.speed(0)
t.dot()
t.goto(-100, -100)
turtle.done()

The error is:

Traceback (most recent call last):
  File "rptTurtle.py", line 12, in <module>
    t = turtle.Turtle()
  File "/usr/lib/python3.7/turtle.py", line 3816, in __init__
    visible=visible)
  File "/usr/lib/python3.7/turtle.py", line 2557, in __init__
    self._update()
  File "/usr/lib/python3.7/turtle.py", line 2660, in _update
    self._update_data()
  File "/usr/lib/python3.7/turtle.py", line 2646, in _update_data
    self.screen._incrementudc()
  File "/usr/lib/python3.7/turtle.py", line 1292, in _incrementudc
    raise Terminator
turtle.Terminator

Where is the problem and how can it be corrected?

2 Answers 2

1

Standalone Python Turtle is not designed to be restarted once it quits. You might work around this by using embedded turtle in a tkinter program. However, it seems simpler to not quit turtle but to reset() the screen to make a new drawing.

The following example puts up your first drawing, and if you click the mouse button on the screen, it will reset the screen and put up your second drawing:

from turtle import Screen, Turtle

def first_drawing():
    turtle = Turtle()
    turtle.speed('fastest')
    turtle.dot()
    turtle.goto(100, 100)

def second_drawing():
    turtle = Turtle()
    turtle.speed('fastest')
    turtle.dot()
    turtle.goto(-100, -100)

def switch(x, y):
    screen.onclick(None)
    screen.reset()
    second_drawing()

screen = Screen()
screen.onclick(switch)

first_drawing()

screen.mainloop()

We could modify this to handle any number of drawings using a list of drawing functions.

Sign up to request clarification or add additional context in comments.

1 Comment

Above code works. But I wanted this to work: first_drawing(); second_drawing()
1

Since you have asked specifically for the "FIRST DRAWING" and "SECOND DRAWING" style execution in your comments, please find below the solutions:

1)

import importlib
import turtle

# FIRST DRAWING: 
t = turtle.Turtle()
t.speed(0)
t.dot()
t.goto(100, 100)
turtle.done()
importlib.reload(turtle)

# SECOND DRAWING: 
t = turtle.Turtle() 
t.speed(0)
t.dot()
t.goto(-100, -100)
turtle.done()

import turtle

# FIRST DRAWING: 
t = turtle.Turtle()
t.speed(0)
t.dot()
t.goto(100, 100)
turtle.done()

# SECOND DRAWING: 
turtle.TurtleScreen._RUNNING=True
t = turtle.Turtle() 
t.speed(0)
t.dot()
t.goto(-100, -100)
turtle.done()

1 Comment

Seems a very good and easy solution. Will certainly try it and post feedback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.