語(yǔ)言:Python
IDE:Python.IDE
1.編寫時(shí)鐘程序,要求根據(jù)時(shí)間動(dòng)態(tài)更新
2.代碼思路
需求:5個(gè)Turtle對(duì)象, 1個(gè)繪制外表盤+3個(gè)模擬表上針+1個(gè)輸出文字
Step1:建立Turtle對(duì)象并初始化
Step2:靜態(tài)表盤繪制
Step3:根據(jù)時(shí)鐘更新表針位置與時(shí)間信息
基本庫(kù):Turtle、datetime
3.代碼段
from turtle import * from datetime import * def Skip(step): penup() forward(step) pendown() def mkHand(name, length): #注冊(cè)Turtle形狀,建立表針Turtle reset() Skip(-length*0.1) begin_poly() forward(length*1.1) end_poly() handForm = get_poly() #注冊(cè)Turtle形狀命令register_shape(name,shape=None) register_shape(name, handForm) def Init(): global secHand, minHand, hurHand, printer mode("logo")# 重置Turtle指向北 #建立三個(gè)表針Turtle并初始化 #第二個(gè)參數(shù)為長(zhǎng)度 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) #建立輸出文字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(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(): #繪制表針的動(dòng)態(tài)顯示 #當(dāng)前時(shí)間 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函數(shù)以控制刷新速度 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)#100ms后繼續(xù)調(diào)用tick def main(): tracer(False) Init() SetupClock(160) tracer(True) Tick() mainloop() if __name__ == "__main__": main()
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。