When using the turtle Module in Python, we can clear the turtle screen by using the clear() function.

import turtle

t = turtle.Turtle()

def draw_square(length):
    for i in range(0,4):
        t.forward(length)
        t.right(90)

draw_square(100)

t.clear()

The turtle module in Python allows us to create graphics easily in our Python code.

We can use the turtle module to make all sorts of designs in Python. For example, we can draw circles and draw squares easily in Python with the turtle module.

When working with our turtle, sometimes it makes sense to want to clear the turtle screen.

We can easily clear the turtle screen with the turtle clear() function. The clear() function clears whatever the turtle has drawn up until that point.

Below is an example in Python of drawing a square and then clearing the turtle screen after the turtle is done.

import turtle

t = turtle.Turtle()

def draw_square(length):
    for i in range(0,4):
        t.forward(length)
        t.right(90)

draw_square(100)

t.clear()

Using Multiple Turtles and the turtle clear() Function in Python

When working with a single turtle, the clear() function clears the entire screen because there is only one shape.

If you have multiple turtles, you can clear certain drawings by clearing a specific turtle.

For example, if you have two turtles, and you want to get rid of what the second turtle has drawn, you can call the clear() function from the second turtle.

Below is an example of using the turtle clear() function with multiple turtles in Python.

import turtle

t1 = turtle.Turtle()
t2 = turtle.Turtle()

def draw_square(t,length):
    for i in range(0,4):
        t.forward(length)
        t.right(90)

t1.up()
t1.setpos(-100,0)
t1.down()

draw_square(t1,100)

t2.up()
t2.setpos(100,0)
t2.down()

draw_square(t2,100)

t2.clear()

turtle clear

As shown above, the second square is cleared from the turtle screen.

Hopefully this article has been useful for you to learn how to clear the screen with the turtle module in python.

Categorized in:

Python,

Last Update: March 1, 2024