#Python制作一個時鍾


#Python制作一個時鍾
import turtle
from datetime import *
def skip(step):
    #移動指定步數
    turtle.penup()
    turtle.forward(step)
    turtle.pendown()

def mk_hand(name,length):
    #繪制指針形狀
    turtle.reset()#清空窗口,重置turtle狀態為起始狀態
    skip(-length*0.1)
    
    turtle.begin_poly()#開始記錄多邊形的頂點,當前位置是多邊形的第一個頂點
    turtle.forward(length*1.1)
    turtle.end_poly()#停止記錄多邊形的頂點,當前烏龜位置是多邊形的最后一個頂點,將與第一個頂點相連
    handForm=turtle.get_poly()#返回最后記錄的多邊形
    turtle.register_shape(name,handForm)#注冊Turtle形狀命令

def setup_clock(radius):
    #繪制表盤
    turtle.reset()
    turtle.pensize(7)
    for i in range(60):
        skip(radius)
        if i%5==0:
            turtle.pencolor("black")
            turtle.forward(20)
            if i==0:
                turtle.write(int(12),align="center",font=("Courier",14,"bold"))
            elif i==30:
                skip(25)
                turtle.write(int(i/5),align="center",font=("Courier",14,"bold"))
                skip(-25)
            elif (i==25 or i==35):
                skip(20)
                turtle.write(int(i/5),align="center",font=("Courier",14,"bold"))
                skip(-20)
            else:
                turtle.write(int(i/5),align="center",font=("Courier",14,"bold"))
            skip(-radius-20)
        else:
            turtle.pencolor("red")
            turtle.dot(5)
            skip(-radius)
        turtle.right(6)#順時鍾移動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()
    sec_hand.setheading(6*t.second)
    min_hand.setheading(6*t.minute)
    hur_hand.setheading(30*t.hour)

    turtle.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()
    turtle.tracer(True)
    turtle.ontimer(tick,1000)


turtle.title("時鍾")
turtle.tracer(False)
turtle.mode("logo")

#繪制指針
mk_hand("secHand",135)
mk_hand("minHand",125)
mk_hand("hurHand",90)
sec_hand=turtle.Turtle()
sec_hand.shape("secHand")
sec_hand.pencolor("red")
min_hand=turtle.Turtle()
min_hand.shape("minHand")
min_hand.pencolor("blue")
hur_hand=turtle.Turtle()
hur_hand.shape("hurHand")
for hand in sec_hand,min_hand,hur_hand:
    hand.shapesize(1,1,3)
    hand.speed(0)

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

#繪制表盤
setup_clock(160)
turtle.tracer(True)
tick()

turtle.done()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM