【问题标题】:How to set up end of turtle after selected time如何在选定的时间后设置海龟的结束
【发布时间】:2020-08-28 15:04:37
【问题描述】:

如何在选定的时间设置海龟的结束。例如如何在 10 秒后结束海龟。

import time
import turtle
t1 = turtle
hours = 10
minutes = 15
seconds = 20

while True:
    t1.clear()
    t1.write(str(hours).zfill(2) + ":" + str(minutes).zfill(2) + ":" + str(seconds).zfill(2))
    seconds = seconds + 1
    time.sleep(1)
    if seconds == 60:
        seconds =0
        minutes = minutes + 1
        if minutes == 60:
            minutes = 0
            hours = hours + 1
            if hours == 24:
                hours = 0
                hours = hours + 1
                time.sleep(4)
                break

【问题讨论】:

  • 请提供更多细节。你的代码应该做什么?你希望最终的结果是什么?
  • 您想在 10 秒后结束程序吗?
  • 是的,我想要

标签: python python-turtle


【解决方案1】:

我们可以使用海龟ontimer()事件来运行时钟限制代码的运行时间:

from turtle import Screen, Turtle

hours = 10
minutes = 15
seconds = 20

run_limit = 10  # in seconds

def tick():
    global hours, minutes, seconds, run_limit

    turtle.clear()

    run_limit -= 1

    if run_limit >= 0:
        time_string = str(hours).zfill(2) + ":" + str(minutes).zfill(2) + ":" + str(seconds).zfill(2)

        turtle.write(time_string, align='center', font=('Arial', 36, 'normal'))

        seconds += 1

        if seconds == 60:
            seconds = 0
            minutes += 1
            if minutes == 60:
                minutes = 0
                hours += 1
                if hours == 24:
                    hours = 0

        screen.ontimer(tick, 1000)

screen = Screen()

turtle = Turtle()
turtle.hideturtle()

tick()

screen.mainloop()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多