猫史档案馆


【Python作品分享】表钟

用户:达摩兔达摩兔查看:0 回复:1 评论:0 创建时间:2022-01-24T12:38:38


【作品展示】

center_image

 

【作品介绍】

可以动的钟哦!

 

【作品源代码】


from turtle import *
from datetime import *

def Skip(step):               
    penup()
    forward(step)
    pendown()

def mkHand(name,length):      
    reset()
    Skip(-length*0.1)
    begin_poly()
    forward(length*1.1)
    end_poly()
    handForm = get_poly()
    register_shape(name,handForm)

def Init():
    global secHand,minHand,hurHand,printer
    mode("logo")             

    mkHand("secHand",125)    
    mkHand("minHand",130)
    mkHand("hurHand",90)

    secHand = Turtle()
    secHand.shape("secHand")
    minHand = Turtle()
    minHand.shape("minHand")
    hurHand = Turtle()
    hurHand.shape("hurHand")

    for hand in secHand,minHand,hurHand:
        hand.shapesize(1,1,3)
        hand.speed(0)

    printer = Turtle()        
    printer.hideturtle()
    printer.penup()

def SetupClock(radius):      
    reset()
    pensize(7)
    for i in range(60):
        Skip(radius)
        if i % 5 == 0:
            forward(20)
            Skip(-radius-20)
        else:
            dot(5)
            Skip(-radius)
        right(6)

def Week(t):
    week = ["星期一", "星期二", "星期三","星期四", "星期五", "星期六", "星期日"]
    return week[t.weekday()]

def Date(t):
    y = t.year
    m = t.month
    d = t.day
    return "%s年%d月%d日" % (y, m, d)
def Tick():
    t = datetime.today()
    second = t.second + t.microsecond * 0.000001
    minute = t.minute + second/60.0
    hour = t.hour + minute/60.0
    secHand.setheading(6*second)
    minHand.setheading(6*minute)
    hurHand.setheading(30*hour)
    tracer(False)
    printer.forward(65)
    printer.write(Week(t),align="center",font=("Courier",14,"bold"))
    printer.back(130)
    printer.write(Date(t),align="center",font=("Courier",14,"bold"))
    printer.home()
    tracer(True)
    ontimer(Tick,100)                

def main():
    tracer(False)
    Init()
    SetupClock(160)
    tracer(True)
    Tick()
    mainloop()
if __name__ == "__main__":
    main()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
yh12i31yh12i31

👍

点赞0


评论