关键词搜索

源码搜索 ×
×

Python实用工具,turtle库,Python实现简易版时钟

发布2021-08-09浏览1090次

详情内容

前言

Python函数库众多,而且在不断更新,所以学习这些函数库最有效的方法,就是阅读Python官方文档。同时借助Google和百度。

turtle库中文文档:

https://docs.python.org/zh-cn/3/library/turtle.html

image.png

开发工具

Python版本:3.6.4

相关模块:

turtle库等python自带的模块。

环境搭建

安装Python并添加到环境变量即可。

原理介绍

利用Turtle库制作一个简易时钟可以分为三步。

第一步:初始化

第二步:创建时钟

第三步,动态显示时钟

初始化需要定义时针分针秒针以及打印文字所需的turtle对象,共四个。其中时针分针秒针这三个turtle对象的定义方式如下:

  1. createHand('second_hand', 150)
  2. createHand('minute_hand', 125)
  3. createHand('hour_hand', 85)
  4. # 秒, 分, 时
  5. second_hand = turtle.Turtle()
  6. second_hand.shape('second_hand')
  7. minute_hand = turtle.Turtle()
  8. minute_hand.shape('minute_hand')
  9. hour_hand = turtle.Turtle()
  10. hour_hand.shape('hour_hand')
  11. for hand in [second_hand, minute_hand, hour_hand]:
  12. hand.shapesize(1, 1, 3)
  13. hand.speed(0)

其中createHand函数用于创建表针(定义形状长度等),其代码实现如下:

  1. '''创建表针turtle'''
  2. def createHand(name, length):
  3. turtle.reset()
  4. move(-length * 0.01)
  5. turtle.begin_poly()
  6. turtle.forward(length * 1.01)
  7. turtle.end_poly()
  8. hand = turtle.get_poly()
  9. turtle.register_shape(name, hand)

然后定义用于打印文字的turtle对象:

  1. # 用于打印日期等文字
  2. printer = turtle.Turtle()
  3. printer.hideturtle()
  4. printer.penup()
  5. createClock(160)

即绘制时钟。其代码实现如下:

  1. '''创建时钟'''
  2. def createClock(radius):
  3. turtle.reset()
  4. turtle.pensize(7)
  5. for i in range(60):
  6. move(radius)
  7. if i % 5 == 0:
  8. turtle.forward(20)
  9. move(-radius-20)
  10. else:
  11. turtle.dot(5)
  12. move(-radius)
  13. turtle.right(6)

为了便于大家理解代码,录了一小段这部分代码运行时的效果图:

图片

动态显示时钟的源代码如下:

  1. '''动态显示表针'''
  2. def startTick(second_hand, minute_hand, hour_hand, printer):
  3. today = datetime.datetime.today()
  4. second = today.second + today.microsecond * 1e-6
  5. minute = today.minute + second / 60.
  6. hour = (today.hour + minute / 60) % 12
  7. # 设置朝向
  8. second_hand.setheading(6 * second)
  9. minute_hand.setheading(6 * minute)
  10. hour_hand.setheading(12 * hour)
  11. turtle.tracer(False)
  12. printer.forward(65)
  13. printer.write(getWeekday(today), align='center', font=("Courier", 14, "bold"))
  14. printer.forward(120)
  15. printer.write('12', align='center', font=("Courier", 14, "bold"))
  16. printer.back(250)
  17. printer.write(getDate(today), align='center', font=("Courier", 14, "bold"))
  18. printer.back(145)
  19. printer.write('6', align='center', font=("Courier", 14, "bold"))
  20. printer.home()
  21. printer.right(92.5)
  22. printer.forward(200)
  23. printer.write('3', align='center', font=("Courier", 14, "bold"))
  24. printer.left(2.5)
  25. printer.back(400)
  26. printer.write('9', align='center', font=("Courier", 14, "bold"))
  27. printer.home()
  28. turtle.tracer(True)
  29. # 100ms调用一次
  30. turtle.ontimer(lambda: startTick(second_hand, minute_hand, hour_hand, printer), 100)

即利用datetime库获取当前的日期与时间,将日期打印在钟表上下两侧,并根据时间调整表针角度,并标明时钟上的点所代表的数字。

注意:为了运行代码时直接呈现出时钟,第一第二步中的代码运行时均设置tracker为False。仅在第三步中设置tracker为True。

文章到这里就结束了,感谢你的观看,下篇文章分享简易音乐播放器

为了感python教程谢读c#教程者们,我想把我vb.net教程最近收藏的一些编程干货分享给大家,回馈每一个读者,希望能帮到你们。

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载