Abhishek Patel
Abhishek Patel

Reputation: 615

turtle delete writing on Screen and Rewrite

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?

Upvotes: 2

Views: 19215

Answers (2)

Robert van den Berg
Robert van den Berg

Reputation: 106

You can also clear this one turtle, instead of clearing the whole screen. Not sure if this fits your requirements, but you might wanna give this a try. I've included a second turtle to demonstrate the rest of the screen stays unaffected.

import turtle
import time

name = "first text"

t1 = turtle.Turtle()
t1.setposition(0, 0)
t1.write(name, font=("Arial", 11, "normal"), align="center")

t2 = turtle.Turtle()
t2.setposition(0, 30)
t2.write(name, font=("Arial", 11, "normal"), align="center")

time.sleep(1)
name = "new value for name 1"
t1.clear()
t1.write(name, font=("Arial", 11, "normal"), align="center")

Upvotes: 2

cdlane
cdlane

Reputation: 41925

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

Upvotes: 6

Related Questions