是否有更有效的方法使用 turtle 模块循环倒数计时器?

Is there a more efficient way of looping a countdown timer using the turtle module?

嘿,我正在尝试制作一个简单的倒数计时器,在我考试前倒计时。它在技术上是可行的,但我对正在编写的“天数”数字进行编码的方式确实效率低下。我不得不这样做,因为当我写了两篇文章时(见下面的代码片段),后者总是闪烁。

text.write(str(delta), align="center", font=("Arial", 50, "normal"))
text2.write(str(delta_time), align="center", font=("Arial", 35, "normal"))

完整代码如下:

from turtle import Screen, Pen, Turtle
import datetime as dt

wn = Screen()
wn.title("Countdown until Mock Exams")
wn.tracer(0)
wn.colormode(255)
# wn.setup(width=300, height=200)

text = Turtle()
text.ht()
text2 = Turtle()
text2.ht()
text2.goto(0, -55)
zzz = 0
date_exams = dt.date(2022, 2, 14)

while True:
    print(zzz)
    # text.clear()
    text2.clear()
    date_now = dt.date.today()
    # print(dt.datetime())

    delta = date_exams-date_now
    delta = str(delta).split(",")
    del delta[-1]
    delta = delta[0]

    x = dt.datetime.today().strftime("%H:%M:%S")
    y = "23:59:59"
    format = "%H:%M:%S"

    delta_time = dt.datetime.strptime(y, format) - dt.datetime.strptime(x, format)
    text2.write(str(delta_time), align="center", font=("Arial", 35, "normal"))

    if zzz == 100 or zzz == 0: # this technically works but PC has to do more work (ie. fans start blowing)
        text.clear()
        text.write(str(delta), align="center", font=("Arial", 50, "normal"))
        zzz = 0
    zzz += 1

非常感谢任何帮助!

您可以使用 time.sleep(1.0) 而不是计算 zzz

from turtle import Screen, Pen, Turtle
import datetime as dt
import time

wn = Screen()
wn.title("Countdown until Mock Exams")
wn.tracer(0)
wn.colormode(255)
# wn.setup(width=300, height=200)

text = Turtle()
text.ht()
text2 = Turtle()
text2.ht()
text2.goto(0, -55)
date_exams = dt.date(2022, 2, 14)

while True:
    text2.clear()
    date_now = dt.date.today()
    # print(dt.datetime())

    delta = date_exams - date_now
    delta = str(delta).split(",")
    del delta[-1]
    delta = delta[0]

    x = dt.datetime.today().strftime("%H:%M:%S")
    y = "23:59:59"
    format = "%H:%M:%S"

    delta_time = dt.datetime.strptime(y, format) - dt.datetime.strptime(x,
                                                                        format)
    text2.write(str(delta_time), align="center", font=("Arial", 35, "normal"))

    text.write(str(delta), align="center", font=("Arial", 50, "normal"))
    time.sleep(1.0)