turtle.done() function in Python

The turtle.done() function starts the event loop. This must be the last statement in a turtle program to keep the window open. It is an alias for mainloop().

Syntax: turtle.done()
Parameters: None.

Let us see an example to implement the turtle.done() function in Python:

Demo38.py

# turtle.done() function in Python
# Code by Studyopedia

import turtle

window = turtle.Screen()
t = turtle.Turtle()

t.forward(100)
t.left(90)
t.forward(100)

turtle.done()  # Keep window open until manually closed

The following is the output:

turtle.done() function in Python

In the above code, we followed the below steps:

  • Import module: import turtle loads the turtle graphics library.
  • Create screen: window = turtle.Screen() opens the drawing window.
  • Create turtle: t = turtle.Turtle() creates the turtle you’ll control.
  • Move forward: t.forward(100) draws a 100-unit line (pen is down by default).
  • Turn left: t.left(90) rotates the turtle 90° counterclockwise.
  • Move forward again: t.forward(100) draws another 100-unit line in the new direction.
  • Keep window open: turtle.done() enters the event loop and keeps the window open until you close it manually.

If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

turtle.exitonclick() function in Python
turtle.showturtle() function in Python
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment