'''
Function:
Python制作简易时钟
Author:
Charles
微信公众号:
TONOW
'''
import turtle
import datetime
'''悬空移动'''
def move(distance):
turtle.penup()
turtle.forward(distance)
turtle.pendown()
'''创建表针turtle'''
def createHand(name, length):
turtle.reset()
move(-length * 0.01)
turtle.begin_poly()
turtle.forward(length * 1.01)
turtle.end_poly()
hand = turtle.get_poly()
turtle.register_shape(name, hand)
'''创建时钟'''
def createClock(radius):
turtle.reset()
turtle.pensize(7)
for i in range(60):
move(radius)
if i % 5 == 0:
turtle.forward(20)
move(-radius-20)
else:
turtle.dot(5)
move(-radius)
turtle.right(6)
'''获得今天是星期几'''
def getWeekday(today):
return ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日'][today.weekday()]
'''获得今天的日期'''
def getDate(today):
return '%s年%s月%s日' % (today.year, today.month, today.day)
'''动态显示表针'''
def startTick(second_hand, minute_hand, hour_hand, printer):
today = datetime.datetime.today()
second = today.second + today.microsecond * 1e-6
minute = today.minute + second / 60.
hour = (today.hour + minute / 60) % 12
# 设置朝向
second_hand.setheading(6 * second)
minute_hand.setheading(6 * minute)
hour_hand.setheading(12 * hour)
turtle.tracer(False)
printer.forward(65)
printer.write(getWeekday(today), align='center', font=("Courier", 14, "bold"))
printer.forward(120)
printer.write('12', align='center', font=("Courier", 14, "bold"))
printer.back(250)
printer.write(getDate(today), align='center', font=("Courier", 14, "bold"))
printer.back(145)
printer.write('6', align='center', font=("Courier", 14, "bold"))
printer.home()
printer.right(92.5)
printer.forward(200)
printer.write('3', align='center', font=("Courier", 14, "bold"))
printer.left(2.5)
printer.back(400)
printer.write('9', align='center', font=("Courier", 14, "bold"))
printer.home()
turtle.tracer(True)
# 100ms调用一次
turtle.ontimer(lambda: startTick(second_hand, minute_hand, hour_hand, printer), 100)
'''开始运行时钟'''
def start():
# 不显示绘制时钟的过程
turtle.tracer(False)
turtle.mode('logo')
createHand('second_hand', 150)
createHand('minute_hand', 125)
createHand('hour_hand', 85)
# 秒, 分, 时
second_hand = turtle.Turtle()
second_hand.shape('second_hand')
minute_hand = turtle.Turtle()
minute_hand.shape('minute_hand')
hour_hand = turtle.Turtle()
hour_hand.shape('hour_hand')
for hand in [second_hand, minute_hand, hour_hand]:
hand.shapesize(1, 1, 3)
hand.speed(0)
# 用于打印日期等文字
printer = turtle.Turtle()
printer.hideturtle()
printer.penup()
createClock(160)
# 开始显示轨迹
turtle.tracer(True)
startTick(second_hand, minute_hand, hour_hand, printer)
turtle.mainloop()
if __name__ == '__main__':
start()

fanxbl957
- 粉丝: 4183
- 资源: 5655
最新资源
- 三段式电流保护Matlab Simulink仿真模型与实验:原理、相间距离保护及整定分析(多版本含操作指南),三段式电流保护Matlab Simulink仿真模型与实验:涵盖模型仿真文件、操作说明、保
- 批量下载源码:编译的Python whl包生成及其自动化源码获取方法,编译的Python二进制whl包批量下载源码解析与实现,编译的二进制Python的whl包批量下载源码 ,编译的二进制; Pyth
- 基于CFD的轴流风叶流体力学分析:高效能设计的策略与探讨,利用CFD计算流体力学分析轴流风叶的性能及优化设计,cfd 计算流体力学 分析轴流风叶 ,关键词:CFD;计算流体力学;轴流风叶;流场分析;气
- Matlab Simulink三闭环直流电机调速系统仿真:深度解析位置环、速度环与电流环的PI控制及PWM调制技术,附H桥正反转控制及详尽说明文档(R2022a版本),Matlab Simulink三
- 基于形状的跨平台模板匹配技术:Windows、Linux、ARM平台通用的C++实现与OpenCV、HALCON的替代方案,自研跨平台C++模板匹配技术:Windows、Linux、ARM通用,基于形
- 基于OpenCV的跨平台形状模板匹配:C++与C#在Windows与Linux下的应用与实现,适用于ARM等不同架构 ,基于OpenCV的跨平台模板匹配算法:C++与C#实现,支持Windows、Li
- 基于灰狼优化的高斯过程回归算法的锂电池SOH估算学习案例:利用NASA锂离子电池老化数据集的实验分析与MATLAB代码实现,基于Matlab的灰狼优化GPR算法锂电池SOH估算学习案例,电池SOH估
- 污水流量远程监测系统的设计
- 杏鲍菇栽培环境因子远程监测系统及模糊PID控制器的设计
- 医院室内空气质量实时监测系统
- 油浸式变压器三维温度场建模及在线温度监测系统研究
- 血液透析治疗仪的控制系统研究
- LCC-MMC三端混合直流系统:基于PSCAD的仿真研究与分析,LCC-MMC三端混合直流系统仿真分析与优化研究:基于PSCAD软件的模拟研究,LCC-MMC三端混合直流系统pscad仿真 ,LCC
- 诱芯诱捕器自动计数系统的研发
- 基于HTML+JavaScript等技术的心理咨询平台设计源码
- 光伏储能并网三端口仿真研究:DC升压与充放电管理、双PI控制及直流母线电压恒定策略探讨,“光伏储能并网三端口仿真:DC升降压、充放电管理、双向转换与稳压控制”,光伏 储能 并网 三端口 仿真 光伏 :
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


