'''
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()

大雄是个程序员
- 粉丝: 4939
- 资源: 571
最新资源
- 从寄存器到编译器:图解C语言代码是如何变成机器指令的.pdf
- 从零打造HTTP服务器:Socket编程全流程解析.pdf
- 从零到Offer:嵌入式开发必学的STM32+C组合拳.pdf
- 从零开发MiniOS:进程调度+文件系统实战.pdf
- 从零到项目实战:30天掌握C语言核心语法与数据结构.pdf
- 从零开发通讯录程序:C语言结构体+文件操作实战教程.pdf
- 从零实现C语言版贪吃蛇:链表+控制台动画开发全解析.pdf
- 从零理解结构体与联合体:5个嵌入式开发实战项目拆解.pdf
- 从零实现贪吃蛇游戏:200行代码掌握链表与文件存储.pdf
- 大学生必读!C语言课程设计常见问题+高分案例解析.pdf
- 代码效率提升300%!GCC编译优化参数完全手册.pdf
- 从零手写贪吃蛇:用C语言实现经典游戏的完整教程.pdf
- 代码重构实战:将面条代码升级为模块化工程的7个技巧.pdf
- 递归vs循环:C语言算法设计中的双刃剑如何选?.pdf
- 颠覆认知!原来main函数有这么多隐藏玩法.pdf
- WP Scheduled Posts Pro v5.1.0开心版 轻松管理WordPress文章发布.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


