Python用turtle画倒计时闹钟
时间: 2023-08-21 16:22:44 浏览: 60
下面是一个简单的Python倒计时闹钟的turtle画图程序:
```python
import turtle
import time
# 初始化画布
win = turtle.Screen()
win.bgcolor('black')
win.setup(width=600, height=400)
win.title('倒计时闹钟')
# 初始化画笔
pen = turtle.Turtle()
pen.hideturtle()
pen.color('white')
pen.penup()
pen.goto(0, 0)
pen.write('倒计时:', align='center', font=('Courier', 24, 'normal'))
# 定义倒计时函数
def countdown(t):
while t > 0:
min, sec = divmod(t, 60)
time_left = '{:02d}:{:02d}'.format(min, sec)
pen.clear()
pen.write('倒计时:' + time_left, align='center', font=('Courier', 24, 'normal'))
time.sleep(1)
t -= 1
pen.clear()
pen.write('时间到!', align='center', font=('Courier', 24, 'normal'))
# 设置倒计时时间
t = 5
# 调用倒计时函数
countdown(t)
# 关闭画布
turtle.done()
```
运行程序后,会在窗口中显示一个倒计时闹钟,倒计时时间为5秒。程序使用turtle库绘制倒计时界面,使用time库进行倒计时。在倒计时过程中,程序会不断更新倒计时的时间,并在画布中显示出来,直到倒计时结束。