python使用turtle庫繪制時(shí)鐘
Python函數(shù)庫眾多,而且在不斷更新,所以學(xué)習(xí)這些函數(shù)庫最有效的方法,就是閱讀Python官方文檔。同時(shí)借助Google和百度。
本文介紹的turtle庫對(duì)應(yīng)的官方文檔地址
繪制動(dòng)態(tài)鐘表的基本思路如下(面向?qū)ο蟮木幊蹋?/p>
使用5個(gè)turtle對(duì)象
1個(gè)turtle:繪制外表盤
3個(gè)turtle:模擬表針行為
1個(gè)turtle:輸出表盤上文字
根據(jù)實(shí)時(shí)時(shí)間使用ontimer()函數(shù)更新表盤畫面,顯示效果如下:
相關(guān)函數(shù)的使用在程序中進(jìn)行了詳細(xì)的注釋,代碼如下:
# -*- coding: utf-8 -*- """ Created on Fri Jan 12 10:43:55 2018 @author: Administrator """ 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() register_shape(name,handform) def init(): global sechand,minhand,hurhand,printer mode("logo") #重置turtle指向北 #建立三個(gè)表針turtle并初始化 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)顯示 t=datetime.today() second=t.second+t.microsecond*0.000001 minute=t.minute+second/60.0 hour=t.hour+second/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)#100ms后繼續(xù)調(diào)用tick def main(): tracer(False) init() setupclock(160) tracer(True) tick() mainloop() main()
運(yùn)行結(jié)果
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
pandas中的DataFrame按指定順序輸出所有列的方法
下面小編就為大家分享一篇pandas中的DataFrame按指定順序輸出所有列的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04Pytorch 抽取vgg各層并進(jìn)行定制化處理的方法
今天小編就為大家分享一篇Pytorch 抽取vgg各層并進(jìn)行定制化處理的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08超級(jí)實(shí)用的8個(gè)Python列表技巧
這篇文章主要介紹了實(shí)用的8個(gè)Python列表技巧,幫助大家更好的理解和學(xué)習(xí)python列表的知識(shí),感興趣的朋友可以了解下2020-08-08python3.8.1+selenium實(shí)現(xiàn)登錄滑塊驗(yàn)證功能
這篇文章主要介紹了python3.8.1+selenium解決登錄滑塊驗(yàn)證的問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05利用numpy實(shí)現(xiàn)一、二維數(shù)組的拼接簡(jiǎn)單代碼示例
這篇文章主要介紹了利用numpy實(shí)現(xiàn)一、二維數(shù)組的拼接簡(jiǎn)單代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12Numpy數(shù)據(jù)轉(zhuǎn)換成image并保存的實(shí)現(xiàn)示例
本文主要介紹了Numpy數(shù)據(jù)轉(zhuǎn)換成image并保存的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12