import turtle
from turtle import *
from datetime import *
def Skip(step):
penup()
forward(step)
pendown()
def mkHand(name, length):
#注冊Turtle形狀,建立表針Turtle
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")# 重置Turtle指向北
#建立三個表針Turtle并初始化
mkHand("secHand", 250)
mkHand("minHand", 190)
mkHand("hurHand", 120)
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)
#建立輸出文字Turtle
printer = Turtle()
printer.hideturtle()
printer.penup()
def SetupClock(radius):
#建立表的外框
reset()
pensize(7)
for i in range(60):
Skip(radius)
if i % 5 == 0:
forward(18)
Skip(-radius-18)
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) #設定朝向,每秒轉動6度
minHand.setheading(6*minute)
hurHand.setheading(30*hour)
tracer(False) #不顯示繪制的程序,直接顯示繪制結果
printer.forward(65)
printer.write(Week(t), align="center",
font=("Courier", 24,))
printer.back(130)
printer.write(Date(t), align="center",
font=("Courier", 30,))
printer.back(50)
printer.write("Gino", align="center",
font=("Simyou", 18, ))
printer.home()
tracer(True)
ontimer(Tick, 1000)#1000ms后繼續呼叫tick
def main():
turtle.color('red')
tracer(False) #使多個繪制物件同時顯示
Init()
SetupClock(300)
tracer(True)
Tick()
mainloop()
if __name__ == "__main__":
main()
#如何把鐘表變成彩色?
#求大神請教!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/43342.html
標籤:其他
上一篇:Z型排列的資料找對應關系
下一篇:新人報道,85后天蝎座老阿姨一枚
