Python實現(xiàn)時鐘顯示效果思路詳解
語言:Python
IDE:Python.IDE
1.編寫時鐘程序,要求根據(jù)時間動態(tài)更新
2.代碼思路
需求:5個Turtle對象, 1個繪制外表盤+3個模擬表上針+1個輸出文字
Step1:建立Turtle對象并初始化
Step2:靜態(tài)表盤繪制
Step3:根據(jù)時鐘更新表針位置與時間信息
基本庫:Turtle、datetime
3.代碼段
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() #注冊Turtle形狀命令register_shape(name,shape=None) register_shape(name, handForm) def Init(): global secHand, minHand, hurHand, printer mode("logo")# 重置Turtle指向北 #建立三個表針Turtle并初始化 #第二個參數(shù)為長度 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(): #繪制表針的動態(tài)顯示 #當前時間 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()
補充:
Python實現(xiàn)時鐘
1.小時鐘獲取當前時間并用打印在Console上
2.上代碼
import time,sys,os while(1): t = time.strftime('%H:%M:%S',time.localtime(time.time())) sys.stdout.write(t+'\b'*10) sys.stdout.flush() time.sleep(0.1) os.system('cls')
3.解釋
第一步:導(dǎo)入time,sys,os模塊
第二部:實現(xiàn)無限循環(huán)
第三步:實現(xiàn)格式化輸出,具體的詳細使用方法,參見:這里
第四部:重定向到Console上
第五步:刷新
第六步:間隔0.1執(zhí)行一次
第七步:清屏
相關(guān)文章
Python操作Redis之設(shè)置key的過期時間實例代碼
這篇文章主要介紹了Python操作Redis之設(shè)置key的過期時間實例代碼,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-01-01django-rest-framework解析請求參數(shù)過程詳解
這篇文章主要介紹了django-rest-framework解析請求參數(shù)過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-07-07linux 下python多線程遞歸復(fù)制文件夾及文件夾中的文件
這篇文章主要介紹了linux 下python多線程遞歸復(fù)制文件夾及文件夾中的文件,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01在Tensorflow中實現(xiàn)leakyRelu操作詳解(高效)
這篇文章主要介紹了在Tensorflow中實現(xiàn)leakyRelu操作詳解(高效),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06