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中的len()函数的使用
Apr 07 Python
python编程线性回归代码示例
Dec 07 Python
Python实现多级目录压缩与解压文件的方法
Sep 01 Python
Python设计模式之工厂方法模式实例详解
Jan 18 Python
python 寻找离散序列极值点的方法
Jul 10 Python
pycharm快捷键汇总
Feb 14 Python
jupyter notebook 参数传递给shell命令行实例
Apr 10 Python
Jupyter Notebook的连接密码 token查询方式
Apr 21 Python
python实战之用emoji表情生成文字
May 08 Python
利用Python第三方库实现预测NBA比赛结果
Jun 21 Python
python中取整数的几种方法
Nov 07 Python
Python可视化学习之seaborn调色盘
Feb 24 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
ThinkPHP单字母函数(快捷方法)使用总结
2014/07/23 PHP
php中Ctype函数用法详解
2014/12/09 PHP
php实现批量修改文件名称的方法
2016/07/23 PHP
Array对象方法参考
2006/10/03 Javascript
需要做特殊处理的DOM元素属性的访问
2010/11/05 Javascript
js 限制数字 js限制输入实现代码
2012/12/04 Javascript
jquery如何把参数列严格转换成数组实现思路
2013/04/01 Javascript
Web Inspector:关于在 Sublime Text 中调试Js的介绍
2013/04/18 Javascript
Javascript setInterval的两种调用方法(实例讲解)
2013/11/29 Javascript
jquery将一个表单序列化为一个对象的方法
2013/12/02 Javascript
jQuery实现切换字体大小的方法
2015/03/10 Javascript
利用jQuery及AJAX技术定时更新GridView的某一列数据
2015/12/04 Javascript
基于JavaScript实现瀑布流效果(循环渐近)
2016/01/27 Javascript
原生js实现放大镜
2017/02/20 Javascript
angular框架实现全选与单选chekbox的自定义
2017/07/06 Javascript
Vue axios 中提交表单数据(含上传文件)
2017/07/06 Javascript
bootstrap3-dialog-master模态框使用详解
2017/08/22 Javascript
移动端H5页面返回并刷新页面(BFcache)的方法
2018/11/06 Javascript
vue把输入框的内容添加到页面的实例讲解
2019/11/11 Javascript
CentOS中使用virtualenv搭建python3环境
2015/06/08 Python
Python实现图像几何变换
2015/07/06 Python
在Python的Django框架中更新数据库数据的方法
2015/07/17 Python
python学习笔记之列表(list)与元组(tuple)详解
2017/11/23 Python
zookeeper python接口实例详解
2018/01/18 Python
mac安装pytorch及系统的numpy更新方法
2018/07/26 Python
OpenCV+python手势识别框架和实例讲解
2018/08/03 Python
python爬虫实例之获取动漫截图
2020/05/31 Python
python语言实现贪吃蛇游戏
2020/11/13 Python
pytorch 实现L2和L1正则化regularization的操作
2021/03/03 Python
用HTML5实现鼠标滚轮事件放大缩小图片的功能
2015/06/25 HTML / CSS
伦敦剧院门票:London Theatre Direct
2018/11/21 全球购物
公司董事长职责
2013/12/12 职场文书
公司员工检讨书
2014/02/08 职场文书
新课培训心得体会
2014/09/03 职场文书
导游词之嵊泗列岛
2019/10/30 职场文书
nginx优化的六点方法
2021/03/31 Servers