问题
In my code, under any function, I do:
t = turtle.Turtle()
t.write(name, font=("Arial", 11, "normal"), align="center")
But when I change the screen, I want to delete this text, and rewrite it somewhere else. I know the "easy way out" of clearing the whole screen. But is there a way to delete just the writing?
I have also tried drawing a white square over the text, but this did not work.
Has anyone tried anything different?
回答1:
At first, I thought this would be a simple matter of going back to the same location and rewriting the same text in the same font but using white ink. Surprisingly, that left a black smudge and it took about 10 overwrites in white to make it presentable. However, I came upon a better solution, use a separate turtle to write the text you want to dispose of and just clear that turtle before rewriting the text in a new position, everything else on the screen, drawn with a different turtle, remains:
import turtle
import time
def erasableWrite(tortoise, name, font, align, reuse=None):
eraser = turtle.Turtle() if reuse is None else reuse
eraser.hideturtle()
eraser.up()
eraser.setposition(tortoise.position())
eraser.write(name, font=font, align=align)
return eraser
t = turtle.Turtle()
t.hideturtle()
t.up()
t.goto(-100,100)
t.write("permanent", font=("Arial", 20, "normal"), align="center")
t.goto(100,100)
eraseble = erasableWrite(t, "erasable", font=("Arial", 20, "normal"), align="center")
time.sleep(1)
eraseble.clear()
t.goto(-100, -100)
erasable = erasableWrite(t, "erasable", font=("Arial", 20, "normal"), align="center", reuse=eraseble)
turtle.done()
来源:https://stackoverflow.com/questions/34823206/turtle-delete-writing-on-screen-and-rewrite