python使用turtle库绘制时钟
作者:蠡1204 时间:2022-09-13 07:06:51
Python函数库众多,而且在不断更新,所以学习这些函数库最有效的方法,就是阅读Python官方文档。同时借助Google和百度。
本文介绍的turtle库对应的官方文档地址
绘制动态钟表的基本思路如下(面向对象的编程):
使用5个turtle对象
1个turtle:绘制外表盘
3个turtle:模拟表针行为
1个turtle:输出表盘上文字
根据实时时间使用ontimer()函数更新表盘画面,显示效果如下:
相关函数的使用在程序中进行了详细的注释,代码如下:
# -*- 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):
#注册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",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=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后继续调用tick
def main():
tracer(False)
init()
setupclock(160)
tracer(True)
tick()
mainloop()
main()
运行结果
来源:https://blog.csdn.net/qq_40006058/article/details/79042230
标签:python,turtle,时钟
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#动态创建Access数据库及密码的方法
2024-01-17 21:37:21
![](https://img.aspxhome.com/file/2023/0/92980_0s.jpg)
python爬虫爬取淘宝商品信息
2023-07-04 10:33:24
![](https://img.aspxhome.com/file/2023/9/75239_0s.jpg)
python编写扎金花小程序的实例代码
2021-01-28 23:09:44
SQL触发器实例讲解
2012-04-13 11:52:48
在Django中预防CSRF攻击的操作
2023-11-11 15:55:13
![](https://img.aspxhome.com/file/2023/8/95378_0s.jpg)
教你如何6秒钟往MySQL插入100万条数据的实现
2024-01-19 02:17:35
![](https://img.aspxhome.com/file/2023/7/105667_0s.jpg)
PyTorch深度学习LSTM从input输入到Linear输出
2022-04-03 23:11:32
![](https://img.aspxhome.com/file/2023/4/114594_0s.png)
Jupyter notebook无法导入第三方模块的解决方式
2023-05-23 08:28:53
![](https://img.aspxhome.com/file/2023/1/72791_0s.jpg)
Python+Opencv答题卡识别用例详解
2021-09-08 22:16:13
![](https://img.aspxhome.com/file/2023/9/64619_0s.jpg)
在python中画正态分布图像的实例
2021-04-13 01:42:23
![](https://img.aspxhome.com/file/2023/1/67231_0s.jpg)
asp源码如何显示数据库字段的结构?
2010-06-08 09:35:00
如何提高ASP的效率?
2010-06-07 20:52:00
Bottle部署web服务及postman接口的方法
2022-06-14 23:38:53
![](https://img.aspxhome.com/file/2023/7/112057_0s.png)
Python实现Word文档样式批量处理
2022-01-13 01:16:05
![](https://img.aspxhome.com/file/2023/5/133315_0s.png)
为自己的网站添加RSS功能
2007-11-05 19:18:00
![](https://img.aspxhome.com/file/UploadPic/20072/20072311555566s.jpg)
零基础也能看懂的python内置csv模块教程
2023-01-06 14:03:16
![](https://img.aspxhome.com/file/2023/2/121692_0s.png)
python selenium操作cookie的实现
2021-12-18 22:54:02
vue组件watch属性实例讲解
2024-05-09 15:19:22
在Windows的Apache服务器上配置对PHP和CGI的支持
2023-10-20 22:12:17
详解Golang如何实现支持随机删除元素的堆
2024-02-22 20:04:53
![](https://img.aspxhome.com/file/2023/4/109754_0s.png)