python trutle库实现动态电子时钟

import turtle
import time
def bg_refresh(): #初始化闹钟背景 (600*200)
    turtle.reset()
    turtle.hideturtle()
    turtle.speed(0)
    turtle.colormode(255)
    turtle.color(0,0,0)
    turtle.begin_fill()
    turtle.up()
    turtle.left(90)
    turtle.fd(160)
    turtle.right(90)
    turtle.pd()
    turtle.fd(300)
    turtle.circle(-50,90)
    turtle.fd(200)
    turtle.circle(-50,90)
    turtle.fd(600)
    turtle.circle(-50,90)
    turtle.fd(200)
    turtle.circle(-50,90)
    turtle.fd(300)
    turtle.end_fill()
def num_write(n):
    turtle.colormode(255)
    turtle.color("red")
    turtle.pensize(15)
    turtle.speed(3)
    if(n==0):
        turtle.fd(30)
        turtle.right(90)
        turtle.fd(150)
        turtle.right(90)
        turtle.fd(60)
        turtle.right(90)
        turtle.fd(150)
        turtle.right(90)
        turtle.fd(30)
    elif(n==1):
        turtle.up()
        turtle.fd(10)
        turtle.pd()
        turtle.right(90)
        turtle.fd(150)
        turtle.left(90)  # 最后状态朝向东
    elif(n==2):
        turtle.fd(60)
        turtle.right(90)
        turtle.fd(75)
        turtle.right(90)
        turtle.fd(60)
        turtle.left(90)
        turtle.fd(75)
        turtle.left(90)
        turtle.fd(60)
    elif(n==3):
        turtle.fd(60)
        turtle.right(90)
        turtle.fd(75)
        turtle.right(90)
        turtle.fd(60)
        turtle.right(180)
        turtle.fd(60)
        turtle.right(90)
        turtle.fd(75)
        turtle.right(90)
        turtle.fd(60)
        turtle.right(180)  # 将方向调整为东
    elif(n==4):
        turtle.right(180)
        turtle.up()
        turtle.fd(30)
        turtle.pd()
        turtle.left(90)
        turtle.fd(75)
        turtle.left(90)
        turtle.fd(60)
        turtle.left(90)
        turtle.up()
        turtle.fd(75)
        turtle.pd()
        turtle.right(180)
        turtle.fd(150)
        turtle.left(90) # 将方向调整为东
    elif(n==5):
        turtle.up()
        turtle.fd(30)
        turtle.pd()
        turtle.right(180)
        turtle.fd(60)
        turtle.left(90)
        turtle.fd(75)
        turtle.left(90)
        turtle.fd(60)
        turtle.right(90)
        turtle.fd(75)
        turtle.right(90)
        turtle.fd(60)
        turtle.right(180) # 将方向调整为东
    elif(n==6):
        turtle.up()
        turtle.fd(30)
        turtle.pd()
        turtle.right(180)
        turtle.fd(60)
        turtle.left(90)
        turtle.fd(75)
        turtle.left(90)
        turtle.fd(60)
        turtle.right(90)
        turtle.fd(75)
        turtle.right(90)
        turtle.fd(60)
        turtle.right(90)
        turtle.fd(75)
        turtle.right(90) # 将方向调整为东
    elif(n==7):
        turtle.right(180)
        turtle.up()
        turtle.fd(30)
        turtle.pd()
        turtle.right(180)
        turtle.fd(60)
        turtle.right(90)
        turtle.fd(150)
        turtle.left(90) # 将方向调整为东
    elif(n==8):
        turtle.up()
        turtle.fd(30)
        turtle.pd()
        turtle.right(180)
        turtle.fd(60)
        turtle.left(90)
        turtle.fd(75)
        turtle.left(90)
        turtle.fd(60)
        turtle.right(90)
        turtle.fd(75)
        turtle.right(90)
        turtle.fd(60)
        turtle.right(90)
        turtle.fd(75)
        turtle.right(90)
        turtle.fd(60)
        turtle.left(90)
        turtle.fd(75)
        turtle.right(90) # 将方向调整为东
    elif(n==9):
        turtle.up()
        turtle.fd(30)
        turtle.pd()
        turtle.right(180)
        turtle.fd(60)
        turtle.left(90)
        turtle.fd(75)
        turtle.left(90)
        turtle.fd(60)
        turtle.left(90)
        turtle.up()
        turtle.fd(75)
        turtle.pd()
        turtle.right(180)
        turtle.fd(150)
        turtle.right(90)
        turtle.fd(60)
        turtle.right(180) # 将方向调整为东

def move(dir):
    turtle.up()
    if(dir==0): #冒号
        turtle.goto(0,40)
        turtle.pd()
        turtle.fd(1)
        turtle.up()
        turtle.goto(0,-40)
        turtle.pd()
        turtle.fd(1)
    elif(dir==1):
        turtle.goto(-250,80)
    elif(dir==2):
        turtle.goto(-130,80)
    elif(dir==3):
        turtle.goto(110,80)
    elif(dir==4):
        turtle.goto(230,80)
    turtle.pd()
hour = str(time.localtime().tm_hour)
minute = str(time.localtime().tm_min)
print(hour,minute)
bg_refresh()
move(1)
if len(hour)!=1:
    num_write(int(hour[0]))
    move(2)
    num_write(int(hour[1]))
else:
    num_write(0)
    move(2)
    num_write(int(hour[0]))
move(0)
move(3)
if len(minute)!=1:
    num_write(int(minute[0]))
    move(4)
    num_write(int(minute[1]))
else:
    num_write(0)
    move(4)
    num_write(int(minute[0]))
while True:
    if(str(time.localtime().tm_hour)!=hour or str(time.localtime().tm_min)!=minute):
        hour = str(time.localtime().tm_hour)
        minute = str(time.localtime().tm_min)
        bg_refresh()
        move(1)
        if len(hour) != 1:
            num_write(int(hour[0]))
            move(2)
            num_write(int(hour[1]))
        else:
            num_write(0)
            move(2)
            num_write(int(hour[0]))
        move(0)
        move(3)
        if len(minute) != 1:
            num_write(int(minute[0]))
            move(4)
            num_write(int(minute[1]))
        else:
            num_write(0)
            move(4)
            num_write(int(minute[0]))
    time.sleep(1)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值