How to hide a turtle icon/pointer in Python
Asked Answered
M

2

19

When using Python Turtle, how do you hide turtle icon(s)/pointer(s) in turtle graphics in Turtle code so that it won't show when testing?

Mortal answered 27/9, 2015 at 4:25 Comment(0)
E
21

The documentation has a section on Visibility:

turtle.hideturtle()
turtle.ht()
Make the turtle invisible. It’s a good idea to do this while you’re in the middle of doing some complex drawing, because hiding the turtle speeds up the drawing observably.

>>> turtle.hideturtle()

Also, you can un-hide the turtle:

turtle.showturtle()
turtle.st()
Make the turtle visible.

>>> turtle.showturtle()

You can also query its visibilty:

turtle.isvisible()
Return True if the Turtle is shown, False if it’s hidden.

>>> turtle.hideturtle()
>>> turtle.isvisible()
False
>>> turtle.showturtle()
>>> turtle.isvisible()
True
Exorbitant answered 27/9, 2015 at 5:51 Comment(0)
Q
7

One more practical method that the other answer failed to address is to set the visible keyword argument to False when defining the Turtle object:

import turtle

my_turtle = turtle.Turtle(visible=False)

Of course, that is for when you want the Turtle to be invisible from the very beginning of the program.

When you define a Turtle object without setting visible to False, there will always be a lightning short moment where the turtle is still visible:


import turtle

my_turtle = turtle.Turtle()
# The Turtle may be visible before the program reaches the line under, depending on the speed of your computer 
my_turtle.hideturtle()

With the visible keyword argument set to False, you can always call my_turtle.showturtle() and my_turtle.hideturtle() in your code where ever the Turtle needs to be visible and hidden again.


Here are all the default turtle settings that you can customize (the settings of interest here are the ones commented with the # RawTurtle):

_CFG = {"width" : 0.5,               # Screen
        "height" : 0.75,
        "canvwidth" : 400,
        "canvheight": 300,
        "leftright": None,
        "topbottom": None,
        "mode": "standard",          # TurtleScreen
        "colormode": 1.0,
        "delay": 10,
        "undobuffersize": 1000,      # RawTurtle
        "shape": "classic",
        "pencolor" : "black",
        "fillcolor" : "black",
        "resizemode" : "noresize",
        "visible" : True,
        "language": "english",        # docstrings
        "exampleturtle": "turtle",
        "examplescreen": "screen",
        "title": "Python Turtle Graphics",
        "using_IDLE": False
       }

Update: I just noticed that cdlane's comment on the other answer pointed out this method, but comments are temporary.

Quintie answered 25/11, 2020 at 19:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.