Hiding the turtle in python turtle drawing

Issue

I am trying to make it so that the turtle is hidden from the beginning of the program but even after putting t.hideturtle() right below where I declare the turtle as variable t the turtle still seems to show up in the middle of the drawing.

import turtle
from random import randint

s = turtle.getscreen()
t = turtle.Turtle()
t.hideturtle()
rx = randint(50,100)
ry = randint(50,100)
width, height = 32,32
s.screensize(width, height)
s.bgcolor("black")

t.goto(0,0)
t.speed(15)

num=10
while num<=1000:
    r = randint(1,5)
    if r == 1:
        t.pencolor("white")
    elif r == 2:
        t.pencolor("#00FFFF")
    elif r == 3:
        t.pencolor("#89CFF0")
    elif r == 4:
        t.pencolor("#0000FF")
    elif r == 5:
        t.pencolor("#00FFFF")
    t.right(25)
    t.circle(num)
    num=num+10
    count=num//10
print("ran",count,"times")

Solution

This was a tricky one.

The key problem here is that your first statement creates one turtle and returns you its screen. That turtle remains visible. Your second statement creates a new turtle, which you hide. Change the order to:

t = turtle.Turtle()
s = t.getscreen()

and it all works as expected.

Answered By – Tim Roberts

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published