I have encountered an issue with the turtle graphics code. Despite trying various methods to hide the turtle, such as using bob.hideturtle()
at different points in the code, the arrow remains visible. Additionally, I attempted to move the arrow to an off-screen location, but this approach did not resolve the problem.
I suspect there may be a small error somewhere in the code that is causing this issue. Could you please help me identify and fix the problem?
import turtle
bob = turtle.Turtle()
scr = turtle.getscreen()
scr.bgcolor("#85B14E")
bob.speed(0)
bob.color("yellow")
turtle.tracer(0)
turtle.hideturtle()
def flower():
for i in range(21):
bob.forward(20)
bob.left(163)
def flowers():
for i in range(49):
bob.penup()
bob.goto(0, 0)
bob.forward(150)
bob.pendown()
flower()
bob.penup()
bob.goto(0, 0)
bob.left(8)
flowers()
turtle.hideturtle()
turtle.update()
bob.goto(1000,10000)
turtle.done()
Here is the link to the image for reference:
Smilingpotato404 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
hideturtle()
method should be on bob
You can do:
from turtle import Screen, Turtle
bob = turtle.Turtle()
scr = bob.getscreen()
or
scr = Screen()
then try:
bob.hideturtle()
getscreen()
is a turtle method, so when you call it with the module name turtle.getscreen()
it applies to the default turtle.
It gets you the screen, but also instantiates the default turtle.
0
Your first problem is import turtle
, which tempts you to call turtle.
functions on the top-level module. turtle.
functions like .getscreen()
and .hideturtle()
, auto-instantate a default turtle, different than bob
or any others you manually created. This is a common source of bugs in turtle graphics programs (see below).
So always use the following import: from turtle import Screen, Turtle
. This way, you’re forced to instantiate a turtle and there’s no way to access the auto-instantiated turtle.
namespace.
Here’s a rewrite:
from turtle import Screen, Turtle
def draw_flower(t: Turtle):
for _ in range(21):
t.forward(20)
t.left(163)
def draw_flowers(t: Turtle):
for _ in range(49):
t.penup()
t.goto(0, 0)
t.forward(150)
t.pendown()
draw_flower(t)
t.penup()
t.goto(0, 0)
t.left(8)
bob = Turtle()
scr = Screen()
scr.bgcolor("#85B14E")
scr.tracer(0)
bob.color("yellow")
bob.hideturtle()
draw_flowers(bob)
scr.update()
scr.exitonclick()
Notes:
- Pass parameters into functions rather than relying on magic global variables. Functions should be self-contained to avoid bugs and to make debugging easier when an issue does arise.
- Function names should only be nouns when they’re getters (i.e., they return the noun). Otherwise, make them verbs. Name them what the function does.
- Try to avoid hardcoded values–it’s OK here, but
draw_flowers
is pretty rigid. Using relative movements (avoidgoto
) and dynamically-computed numbers will make it easier to extend and reuse in the future. bob.goto(1000,10000)
is called afterturtle.update()
, so its result is never reflected on the screen. Make sure movements happen before calling.update()
.
Related questions about “ghost turtles”:
- Why is there a second Turtle?
- Why is there a little arrow in the middle of the screen?
- Little black square in center of screen after each restart in turtle graphics pong game
- Third Clone Of The Turtle
4
You need to call the hideturtle()
method on your Turtle instance bob
.
You need to change
turtle.hideturtle()
into
bob.hideturtle()
You claim to have tried it in your question but the actual code shows a different thing.
1