'Turtle Text Flashing

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()


Solution 1:[1]

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?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cdlane