How to Clear the Screen with Python Turtle?

Estimated read time 1 min read

To clear the screen in Python Turtle graphics, you can use the turtle module’s TurtleScreen object and its clear() method. Here’s an example:

import turtle

# Create a Turtle screen object
screen = turtle.Screen()

# Clear the screen
screen.clear()

# Example usage: Draw a square after clearing the screen
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)

# Wait for the user to close the window
turtle.done()

In this example, the screen.clear() method is called to clear the screen before drawing a new shape. After clearing the screen, you can use the Turtle graphics commands to draw any desired shapes or patterns.

Make sure to call screen.clear() before drawing any new graphics if you want to start with a blank canvas.

Note that you need to import the turtle module and create a Screen object before using Turtle graphics. The turtle.done() function is called at the end to keep the turtle window open until the user closes it.

By using screen.clear() in your Python Turtle program, you can clear the screen to start fresh for each new drawing or animation.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply