Question

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)
Était-ce utile?

La solution

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.

Autres conseils

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top