0

When I attempt to put text over a turtle in the python turtle module, it flashes. Any solutions?

import turtle

s = turtle.Screen()
s.setup(width=500,height=600)

c = turtle.Turtle()
c.shapesize(stretch_len=5,stretch_wid=5)
c.goto(0,0)
c.shape("square")

pen = turtle.Turtle()
pen.hideturtle()
pen.goto(0,0)
pen.color("red")

while True:
  pen.write("hello!")
  s.update()
cdlane
  • 40,441
  • 5
  • 32
  • 81
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Mar 14 '22 at 23:57

1 Answers1

0

Although I don't see the flashing on my screen, my guess is your problem is related to this bad idiom:

while True:
  # ...
  s.update()

We need neither the while True: (which has no place in an event-driven environment like turtle) nor the call to update() (which is not needed given no previous call to tracer()). Let's rewrite this as turtle code:

from turtle import Screen, Turtle

screen = Screen()
screen.setup(width=500, height=600)

turtle = Turtle()
turtle.hideturtle()
turtle.shapesize(5)
turtle.shape('square')
turtle.stamp()  # stamp a square so we can reuse turtle

pen = Turtle()
pen.hideturtle()
pen.color("red")
pen.write("Hello!", align='center', font=('Arial', 16, 'normal'))

screen.exitonclick()

Does that solve your problem?

cdlane
  • 40,441
  • 5
  • 32
  • 81