hamidfzm
hamidfzm

Reputation: 4685

Fading Text in Python Turtle

Is there anyway to fade a text for showing and hiding. Or clear part of screen without cleaning other drawings.

import turtle
#fade this text
turtle.write("Hello")
#clear some shape
turtle.fd(100)

Upvotes: 2

Views: 3429

Answers (2)

Eric Leschinski
Eric Leschinski

Reputation: 153993

How to simulate fading of text labels from a python turtle without disrupting the other stamps:

You have to do this yourself by deleting and re-drawing the stamp/text with a lighter color. The following example creates two turtles, one called Alex that moves around and another deep clone of alex called alex_text. Alex text is written to screen and then cleared. Then the alex_text is just another turtle that follows alex around to the new location which gives us a handle to clear it without disrupting alex. So Alex's stamps say around and the others were removed and re-drawn with a color closer and closer to white until it is white on white.

import turtle
import time
alex = turtle.Turtle()
alex_text = turtle.Turtle()
alex_text.goto(alex.position()[0], alex.position()[1])
alex_text.write("hello")
time.sleep(1)
alex_text.clear()
alex.goto(100, 100)
alex_text.goto(alex.position()[0], alex.position()[1])
alex_text.write("hello2")
time.sleep(1)

The above example fades from black to white in one step, the example below fades the text from black to white in 5 steps with a 1 second delay.

import turtle
import time

alex = turtle.Turtle()
alex_text = turtle.Turtle()
alex_text.goto(alex.position()[0], alex.position()[1])

alex_text.pencolor((0, 0, 0))
alex_text.write("hello")
time.sleep(1)
alex_text.clear()

alex_text.pencolor((.1, .1, .1))
alex_text.write("hello")
time.sleep(1)

alex_text.pencolor((.5, .5, .5))
alex_text.write("hello")
time.sleep(1)

alex_text.pencolor((.8, .8, .8))
alex_text.write("hello")
time.sleep(1)

alex_text.pencolor((1, 1, 1))
alex_text.write("hello")
time.sleep(1)

alex_text.clear()
time.sleep(1)

Read up about the pencolor method, the write method, the clear method, the position method and goto method here: https://docs.python.org/3.3/library/turtle.html

Upvotes: 0

furas
furas

Reputation: 142681

There is no function to fade text and clear shape.

You can write text on text with new color but it is not ideal.

import turtle

turtle.colormode(255)

for i in range(0,255,15):
    turtle.pencolor(i,i,i)
    turtle.write("Hello")
    turtle.delay(100)

If you have white background you could clear shape by drawing the same shape with white color. But it is too much work.

Upvotes: 2

Related Questions