網(wǎng)站首頁 編程語言 正文
源代碼
python引入兩個內(nèi)置庫turtle和datetime即可制作一個實(shí)時鐘表,代碼如下:
# 用turtle畫時鐘 # 以自定義shape的方式實(shí)現(xiàn) import turtle as t import datetime as d def skip(step): # 抬筆,跳到一個地方 t.penup() t.forward(step) t.pendown() def drawClock(radius): # 畫表盤 t.speed(0) t.mode("logo") # 以Logo坐標(biāo)、角度方式 t.hideturtle() t.pensize(7) t.home() # 回到圓點(diǎn) for j in range(60): skip(radius) if (j % 5 == 0): t.forward(20) skip(-radius - 20) else: t.dot(5) skip(-radius) t.right(6) def makePoint(pointName, len): # 鐘的指針,時針、分針、秒針 t.penup() t.home() t.begin_poly() t.back(0.1 * len) t.forward(len * 1.1) t.end_poly() poly = t.get_poly() t.register_shape(pointName, poly) # 注冊為一個shape def drawPoint(): # 畫指針 global hourPoint, minPoint, secPoint, fontWriter makePoint("hourPoint", 100) makePoint("minPoint", 120) makePoint("secPoint", 140) hourPoint = t.Pen() # 每個指針是一只新turtle hourPoint.shape("hourPoint") hourPoint.shapesize(1, 1, 6) minPoint = t.Pen() minPoint.shape("minPoint") minPoint.shapesize(1, 1, 4) secPoint = t.Pen() secPoint.shape("secPoint") secPoint.pencolor('red') fontWriter = t.Pen() fontWriter.pencolor('gray') fontWriter.hideturtle() def getWeek(weekday): weekName = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日'] return weekName[weekday] def getDate(year, month, day): return "%s-%s-%s" % (year, month, day) def getRealtime(): curr = d.datetime.now() curr_year = curr.year curr_month = curr.month curr_day = curr.day curr_hour = curr.hour curr_minute = curr.minute curr_second = curr.second curr_weekday = curr.weekday() t.tracer(False) secPoint.setheading(360 / 60 * curr_second) minPoint.setheading(360 / 60 * curr_minute) hourPoint.setheading(360 / 12 * curr_hour + 30 / 60 * curr_minute) fontWriter.clear() fontWriter.home() fontWriter.penup() fontWriter.forward(80) # 用turtle寫文字 fontWriter.write(getWeek(curr_weekday), align="center", font=("Courier", 14, "bold")) fontWriter.forward(-160) fontWriter.write(getDate(curr_year, curr_month, curr_day), align="center", font=("Courier", 14, "bold")) t.tracer(True) print(curr_second) t.ontimer(getRealtime, 1000) # 每隔1000毫秒調(diào)用一次realTime() def main(): t.tracer(False) drawClock(160) drawPoint() getRealtime() t.tracer(True) t.mainloop() if __name__ == '__main__': main()
引用模塊:turtle,datetime
定義函數(shù):
- skip():重復(fù)指定動作
- drawClock():畫表盤
- drawPoint():畫指針
- getWeek():顯示星期
- getDate():顯示日期
- getRealtime():顯示實(shí)時時間
- main():主函數(shù)
運(yùn)行效果如下:
打包exe
也可打包為exe,這樣在沒有編譯環(huán)境的情況下也可以運(yùn)行(發(fā)給你的小伙伴展示一下你的技術(shù),豈不美哉) 效果如下(不會打包看pyinstaller打包exe詳細(xì)教程) 這里就不做演示了,直接上效果圖
以上就是利用turtle庫和datetime庫制作實(shí)時鐘表
原文鏈接:https://blog.csdn.net/xqe777/article/details/123161248
相關(guān)推薦
- 2022-11-07 react使用websocket實(shí)時通信方式_React
- 2022-05-14 pytorch中with?torch.no_grad():的用法實(shí)例_python
- 2022-04-05 C#反射調(diào)用拓展類方法實(shí)例代碼_C#教程
- 2023-04-01 Android自定義View?Material?Design理念詳解_Android
- 2023-06-17 pytorch?簡介及常用工具包展示_python
- 2022-03-23 C++實(shí)現(xiàn)AVL樹的基本操作指南_C 語言
- 2022-07-01 python神經(jīng)網(wǎng)絡(luò)ShuffleNetV2模型復(fù)現(xiàn)詳解_python
- 2022-09-30 python實(shí)現(xiàn)圖像降噪_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支