0
点赞
收藏
分享

微信扫一扫

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

前言

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

turtle库中文文档:

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

开发工具

Python版本:3.6.4

相关模块:

turtle库等python自带的模块。

环境搭建

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

原理介绍

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

第一步:初始化

第二步:创建时钟

第三步,动态显示时钟

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

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)

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

'''创建表针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)

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

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

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

'''创建时钟'''
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 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)

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

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

文章到这里就结束了,感谢你的观看,关注我每天分享Python小工具系列,下篇文章分享简易音乐播放器

为了感谢读者们,我想把我最近收藏的一些编程干货分享给大家,回馈每一个读者,希望能帮到你们。(点击干货主要有获取)

点击干货主要有获取

① 2000多本Python电子书(主流和经典的书籍应该都有了)

② Python标准库资料(最全中文版)

③ 项目源码(四五十个有趣且经典的练手项目及源码)

④ Python基础入门、爬虫、web开发、大数据分析方面的视频(适合小白学习)

⑤ Python学习路线图(告别不入流的学习)

⑥ 两天的Python爬虫训练营直播权限

All done~完整源代码+干货详见个人简介或者私信获取相关文件。。

举报

相关推荐

0 条评论