Ora

How to keep the turtle window open in Python?

Published in Turtle Window Control 5 mins read

To keep the turtle graphics window open in Python, you must include a specific statement at the end of your script that pauses program execution. This prevents the window from closing instantly after your turtle finishes drawing.

By default, Python closes the turtle window immediately once the program finishes running. This can be frustrating as any cool graphics you've created disappear before you get a chance to fully appreciate them. To prevent this, you simply need to add a command at the very end of your program that tells Python to wait before it concludes.


Why Does the Turtle Window Close Automatically?

When a Python script completes its execution, all resources it used, including graphical windows, are typically closed and cleaned up by the operating system. For turtle graphics, this means the visual window disappears as soon as the last drawing command is processed and there's nothing left for the program to do. To keep the window visible, we need to instruct the program to wait before it officially exits.


Key Methods to Keep the Turtle Window Open

Several functions are available in the turtle module to ensure the window remains open. These methods essentially start an event loop that listens for user input or simply keeps the window active until explicitly closed.

1. Using turtle.done() or turtle.mainloop()

The most common and recommended way to keep the turtle window open is by calling turtle.done() or turtle.mainloop() at the end of your script. These functions start the Tkinter event loop, which allows the window to remain open and responsive to events (like resizing or closing) until you manually close it.

  • turtle.done(): A convenience function that is often an alias for turtle.mainloop().
  • turtle.mainloop(): This is the underlying function that starts the Tkinter event loop.

Example:

import turtle

# Create a turtle screen
wn = turtle.Screen()
wn.bgcolor("lightgreen")
wn.title("My First Turtle Drawing")

# Create a turtle
alex = turtle.Turtle()
alex.color("blue")
alex.pensize(3)

# Make the turtle draw something
for i in range(4):
    alex.forward(100)
    alex.left(90)

# Keep the window open until manually closed
turtle.done()
# Alternatively, you could use:
# wn.mainloop()

2. Using turtle.exitonclick()

This method is particularly useful if you want the window to close when the user clicks anywhere on the turtle screen. It also starts an event loop, similar to turtle.done(), but with the added functionality of closing upon a click. If you've created a screen object (e.g., screen = turtle.Screen()), you would use screen.exitonclick().

Example:

import turtle

# Create a turtle screen
screen = turtle.Screen()
screen.bgcolor("lightblue")
screen.title("Click to Exit")

# Create a turtle
pen = turtle.Turtle()
pen.shape("triangle")
pen.color("red")

# Make the turtle draw a shape
for _ in range(3):
    pen.forward(80)
    pen.left(120)

# Keep the window open until the user clicks on it
screen.exitonclick()

3. Using input() (Less Common for Turtle Graphics)

While not specifically designed for turtle graphics, the input() function in Python can pause any program's execution, including those with a turtle window, until the user presses Enter in the console. This method is less ideal for turtle as it doesn't utilize the Tkinter event loop, meaning the window might not be as responsive (e.g., to resizing) while waiting. However, it's a generic way to pause a script.

Example:

import turtle

# Create a turtle and draw
artist = turtle.Turtle()
artist.circle(50)

# Keep the window open until Enter is pressed in the console
input("Press Enter to close the window...")

Choosing the Right Method

Method Description Use When...
turtle.done() Starts the Tkinter event loop, keeping the window open until manually closed by the user. You want the window to stay open indefinitely until the user closes it.
turtle.mainloop() The core Tkinter event loop function; functionally similar to turtle.done(). Same as turtle.done(), often preferred for clarity with Screen objects.
turtle.exitonclick() Starts the event loop and closes the window specifically when the user clicks anywhere on the screen. You want an interactive way for the user to close the window.
input() Pauses the Python script until the user presses Enter in the console, indirectly keeping the window open. You need a simple, console-based pause and don't require Tkinter event handling.

Practical Insights

  • Placement is Key: Always place the pause statement (turtle.done(), screen.exitonclick(), etc.) as the very last line of your turtle graphics script. Any code after this line will not execute until the window is closed.
  • Event Loop Benefits: turtle.done() and turtle.exitonclick() are superior because they activate the Tkinter event loop. This makes the window more interactive and responsive, allowing it to handle events like closing, minimizing, or maximizing properly.
  • Debugging: When developing complex turtle programs, keeping the window open is crucial for observing the final state of your drawings and debugging any visual anomalies.

By implementing one of these simple statements, you can ensure your creative turtle graphics remain on screen for as long as you need them. For more in-depth information on the turtle module, refer to the Python Turtle Module Documentation.