'''
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
- 粉丝: 3314
- 资源: 1488
最新资源
- 数据结构与算法(附代码详解)
- YOLO人脸口罩佩戴目标检测数据集1类别(1000图片)+yolo、voc、coco三种标签+环境搭建+参考教程+划分脚本.rar
- redis-6.2.6.tar.gz
- github-explore-sponsors-for-a0983627353-2024-12-13.xlsx
- ed48209c0d8674ea9baf74626db7638e_1736161799806.mp4
- 基于Matlab/Simulink的控制系统仿真实践-《计算机仿真基础》课程报告解析
- 基于MATLAB的自动控制系统仿真与校正设计课程-2022级电气工程学生必备技能培养
- 【2024第三期“编程打卡”训练 第4题】
- 地理计算方法之诺西贝岛雷暴临近预报的数据分析与模型构建
- 美、英、法、德、意大利和西班牙的地图json数据
- 基于pytorch实现minist手写数字识别源码+文档说明(高分项目).zip
- 1..1行列式的定义.ppt
- 基于pytorch实现minist手写数字识别源码(高分项目).zip
- 金融投资组合优化中基于一致模糊环境的多期选择模型研究及其应用
- dbeaver-ce-latest-x86-64-setup-24.3.2.exe
- STM32MP157使用中断控制按键控制拓展板LED灯
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈