下面是用Python实现的代码:

import time

countdown = 60  # 倒计时时间,单位为秒

while countdown >= 0:
    minutes, seconds = divmod(countdown, 60)  # 分离出分钟和秒钟
    timeformat = '{:02d}:{:02d}'.format(minutes, seconds)  # 格式化显示
    print(timeformat, end='\r')  # 在同一行输出,覆盖之前的输出
    time.sleep(1)  # 延时1秒
    countdown -= 1

运行结果如下:

00:60
00:59
00:58
...
00:01
00:00

每秒钟输出一次剩余时间,直到倒计时结束。

使用定时器实现不带按键的 60s 倒计时59~00显示的计时时间间隔为 1s仿真一开始就循环进行 60s 倒计时

原文地址: https://www.cveoy.top/t/topic/cxTW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录