python使用turtle库绘制时钟


Posted in Python onMarch 25, 2020

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

本文介绍的turtle库对应的官方文档地址

绘制动态钟表的基本思路如下(面向对象的编程):

使用5个turtle对象
1个turtle:绘制外表盘
3个turtle:模拟表针行为
1个turtle:输出表盘上文字

根据实时时间使用ontimer()函数更新表盘画面,显示效果如下:

相关函数的使用在程序中进行了详细的注释,代码如下:

# -*- coding: utf-8 -*-
"""
Created on Fri Jan 12 10:43:55 2018

@author: Administrator
"""

from turtle import *
from datetime import *

def skip(step):
 penup()
 forward(step)
 pendown()

def mkhand(name,length):
 #注册turtle形状,建立表针turtle
 reset()
 skip(-length*0.1)
 begin_poly()
 forward(length*1.1)
 end_poly()
 handform=get_poly()
 register_shape(name,handform)

def init():
 global sechand,minhand,hurhand,printer
 mode("logo")
 #重置turtle指向北
 #建立三个表针turtle并初始化
 mkhand("sechand",125)
 mkhand("minhand",130)
 mkhand("hurhand",90)
 sechand=Turtle()
 sechand.shape("sechand")
 minhand=Turtle()
 minhand.shape("minhand")
 hurhand=Turtle()
 hurhand.shape("hurhand")
 for hand in sechand,minhand,hurhand:
  hand.shapesize(1,1,3)
  hand.speed(0)
 #建立输出文字turtle
 printer = Turtle()
 printer.hideturtle()
 printer.penup()

def setupclock(radius):
 #建立表的外框
 reset()
 pensize(7)
 for i in range(60):
  skip(radius)
  if i %5==0:
   forward(20)
   skip(-radius-20)
  else:
   dot(5)
   skip(-radius)
  right(6)

def week(t):
 week=["星期一","星期二","星期三","星期四","星期五","星期六","星期日"]
 return week[t.weekday()]

def date(t):
 y=t.year
 m=t.month
 d=t.day
 return "%s %d %d" %(y,m,d)

def tick():
 #绘制表针的动态显示
 t=datetime.today()
 second=t.second+t.microsecond*0.000001
 minute=t.minute+second/60.0
 hour=t.hour+second/60.0
 sechand.setheading(6*second)
 minhand.setheading(6*minute)
 hurhand.setheading(30*hour)
 tracer(False)
 printer.forward(65)
 printer.write(week(t),align="center",font=("Courier",14,"bold"))
 printer.back(130)
 printer.write(date(t),align="center",font=("Courier",14,"bold"))
 printer.home()
 tracer(True)
 ontimer(tick,100)#100ms后继续调用tick

def main():
 tracer(False)
 init()
 setupclock(160)
 tracer(True)
 tick()
 mainloop()
main()

运行结果

python使用turtle库绘制时钟

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
跟老齐学Python之不要红头文件(1)
Sep 28 Python
在Lighttpd服务器中运行Django应用的方法
Jul 22 Python
使用Python操作MySQL的一些基本方法
Aug 16 Python
Python 使用SMTP发送邮件的代码小结
Sep 21 Python
基于python 字符编码的理解
Sep 02 Python
python 实现一个贴吧图片爬虫的示例
Oct 12 Python
示例详解Python3 or Python2 两者之间的差异
Aug 23 Python
python实现任意位置文件分割的实例
Dec 14 Python
python通过移动端访问查看电脑界面
Jan 06 Python
基于PyTorch的permute和reshape/view的区别介绍
Jun 18 Python
pytorch 多分类问题,计算百分比操作
Jul 09 Python
安装pytorch时报sslerror错误的解决方案
May 17 Python
Python日期时间对象转换为字符串的实例
Jun 22 #Python
python pandas 对时间序列文件处理的实例
Jun 22 #Python
python使用turtle绘制分形树
Jun 22 #Python
python递归函数绘制分形树的方法
Jun 22 #Python
使用pandas模块读取csv文件和excel表格,并用matplotlib画图的方法
Jun 22 #Python
Python使用pandas处理CSV文件的实例讲解
Jun 22 #Python
python处理csv中的空值方法
Jun 22 #Python
You might like
php实现无限级分类实现代码(递归方法)
2011/01/01 PHP
PHP中ob_start函数的使用说明
2013/11/11 PHP
ThinkPHP的I方法使用详解
2014/06/18 PHP
详解WordPress中添加和执行动作的函数使用方法
2015/12/29 PHP
PHP使用Session实现上传进度功能详解
2019/08/06 PHP
jquery 将disabled的元素置为enabled的三种方法
2009/07/25 Javascript
Google的跟踪代码 动态加载js代码方法应用
2012/11/12 Javascript
在Iframe中获取父窗口中表单的值(示例代码)
2013/11/22 Javascript
JavaScript实现的Tween算法及缓冲特效实例代码
2015/11/03 Javascript
微信小程序开发之视频播放器 Video 弹幕 弹幕颜色自定义实例
2016/12/08 Javascript
jquery Form轻松实现文件上传
2017/05/24 jQuery
angular4+百分比进度显示插件用法示例
2019/05/05 Javascript
详解mpvue中使用vant时需要注意的onChange事件的坑
2019/05/16 Javascript
解决layer弹出层中表单不起作用的问题
2019/09/09 Javascript
微信小程序开发之转发分享功能
2019/10/22 Javascript
vue移动端使用appClound拉起支付宝支付的实现方法
2019/11/21 Javascript
js实现验证码干扰(静态)
2021/02/22 Javascript
使用C语言来扩展Python程序和Zope服务器的教程
2015/04/14 Python
Django框架实现分页显示内容的方法详解
2019/05/10 Python
Python使用scipy模块实现一维卷积运算示例
2019/09/05 Python
布隆过滤器的概述及Python实现方法
2019/12/08 Python
Python imageio读取视频并进行编解码详解
2019/12/10 Python
python的slice notation的特殊用法详解
2019/12/27 Python
python基于celery实现异步任务周期任务定时任务
2019/12/30 Python
django 装饰器 检测登录状态操作
2020/07/02 Python
利用keras使用神经网络预测销量操作
2020/07/07 Python
python七种方法判断字符串是否包含子串
2020/08/18 Python
英国最红的高街时尚品牌:Topshop
2016/08/05 全球购物
很酷的小工具和电子产品商城:GearBest
2016/11/19 全球购物
委托公证书范本
2014/04/03 职场文书
市级文明单位申报材料
2014/05/07 职场文书
捐助贫困学生倡议书
2014/05/16 职场文书
教师节倡议书
2014/08/30 职场文书
毕业设计致谢词
2015/05/14 职场文书
健康教育主题班会
2015/08/14 职场文书
html中显示特殊符号(附带特殊字符对应表)
2021/06/21 HTML / CSS