When using the turtle Module in Python, we can hide the turtle by using the hideturtle() function.

import turtle

t = turtle.Turtle()

def draw_circle(radius):
    t.circle(radius)

draw_circle(100)

t.hideturtle()

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 shapes 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 hide a turtle.

We can easily hide a turtle in Python with the hideturtle() function.

Below is an example of drawing a circle and then hiding the turtle after the turtle is done in Python.

import turtle

t = turtle.Turtle()

def draw_circle(radius):
    t.circle(radius)

draw_circle(100)

t.hideturtle()

How to Initialize and Hide Turtle in Python

Another way to hide a turtle in Python is to pass the ‘False’ to the keyword ‘visible’ when initializing the turtle.

This will create a hidden turtle in your turtle window.

Below is the Python code to initialize a hidden turtle.

import turtle

t = turtle.Turtle(visible=False)

How to Check if a Turtle is Visible in Python

To check if a turtle is visible or not, use the turtle isvisible() function. The isvisible() function returns True if the turtle is visible and False if the turtle is hidden.

Below is an example showing how to use the Python turtle isvisible() function to check if a turtle is visible or not.

import turtle

t = turtle.Turtle(visible=False)

print(t.is_visible())

t.showturtle()

print(t.is_visible())

#Output:
False 
True

How to Show a Hidden Turtle in Python

There are also turtle functions which allow us to show hidden turtles. We can use the showturtle() function to show a hidden turtle.

Below is an example of drawing a circle with an invisible turtle and then showing the turtle after completion in Python.

import turtle

t = turtle.Turtle(visible=False)

def draw_circle(radius):
    t.circle(radius)

draw_circle(100)

t.showturtle()

Just like with the hideturtle() function, we can use a shorter function name to show a turtle. You can use st(), the short version of showturtle(), as shown below.

import turtle

t = turtle.Turtle(visible=False)

def draw_circle(radius):
    t.circle(radius)

draw_circle(100)

t.st()

Hopefully this article has been useful for you to hide a turtle in your Python programs.

Categorized in:

Python,

Last Update: March 15, 2024