• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / How to Clear Turtle Screen in Python with clear() Function

How to Clear Turtle Screen in Python with clear() Function

February 23, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  Using Python to Check if Number is Divisible by Another Number
  • 2.  Touch File Python – How to Touch a File Using Python
  • 3.  Calculate Distance Between Two Points in Python
  • 4.  How to Serialize a Model Object with a List of Lists Using Django Rest Framework in Python
  • 5.  How to Check if Tuple is Empty in Python
  • 6.  Cartesian Product of Two Lists in Python
  • 7.  Using Python to Flatten Array of Arrays
  • 8.  Create List of Odd Numbers in Range with Python
  • 9.  Using Python to Remove Commas from String
  • 10.  Python Random Boolean – How to Generate Random Boolean Values

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x