问题描述
我想实现一个这样的流程
这是我的代码:
import turtle
import time
snakes = turtle.Turtle()
snakes.ht()
snakes.shape("square")
snakes.up()
time.sleep(1)
snakes.color("Red")
snakes.stamp()
# turtle.update()
time.sleep(1)
snakes.clearstamps(-1)
turtle.done()
当我运行这段代码时,红色方块根本没有出现。似乎红色方块已被“clearstamps()”删除。但是,我在删除之前采取了第 3 步延迟 1 秒。
经过一些尝试,我发现如果我在创建图章后添加更新(只是上面程序的注释部分),程序会成功运行,删除前有1秒的延迟。
我想知道为什么要添加这个 update(),因为我没有关闭程序中的跟踪器。
感谢您的帮助!
解决方法
我会避免在海龟中使用 time.sleep()
,因为它会干扰事件循环,而是使用海龟自己的 ontimer()
方法:
from turtle import Screen,Turtle
def action():
stamp = turtle.stamp()
screen.ontimer(lambda: turtle.clearstamp(stamp),1000)
turtle = Turtle()
turtle.hideturtle()
turtle.color('red')
turtle.shape('square')
screen = Screen()
screen.ontimer(action,1000)
screen.mainloop()