*【Python】【demo实验30】【练习实例】【使用Turtle实现实时时钟效果】
目的:
使用Turtle实现实时时钟效果
源代码:
# encoding=utf-8
# -*- coding: UTF-8 -*- import turtle
from datetime import * # 抬起画笔,向前运动一段距离放下
def Skip(step):
turtle.penup()
turtle.forward(step)
turtle.pendown() def mkHand(name, length):
# 注册Turtle形状,建立表针Turtle
turtle.reset()
Skip(-length * 0.1)
# 开始记录多边形的顶点。当前的乌龟位置是多边形的第一个顶点。
turtle.begin_poly()
turtle.forward(length * 1.1)
# 停止记录多边形的顶点。当前的乌龟位置是多边形的最后一个顶点。将与第一个顶点相连。
turtle.end_poly()
# 返回最后记录的多边形。
handForm = turtle.get_poly()
turtle.register_shape(name, handForm) def Init():
global secHand, minHand, hurHand, printer
# 重置Turtle指向北
turtle.mode("logo")
# 建立三个表针Turtle并初始化
mkHand("secHand", 135)
mkHand("minHand", 125)
mkHand("hurHand", 90)
secHand = turtle.Turtle()
secHand.shape("secHand")
minHand = turtle.Turtle()
minHand.shape("minHand")
hurHand = turtle.Turtle()
hurHand.shape("hurHand") for hand in secHand, minHand, hurHand:
hand.shapesize(1, 1, 3)
hand.speed(0) # 建立输出文字Turtle
printer = turtle.Turtle()
# 隐藏画笔的turtle形状
printer.hideturtle()
printer.penup() def SetupClock(radius):
# 建立表的外框
turtle.reset()
turtle.pensize(7)
for i in range(60):
Skip(radius)
if i % 5 == 0:
turtle.forward(20)
Skip(-radius - 20) Skip(radius + 20)
if i == 0:
turtle.write(int(12), align="center", font=("Courier", 14, "bold"))
elif i == 30:
Skip(25)
turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))
Skip(-25)
elif (i == 25 or i == 35):
Skip(20)
turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))
Skip(-20)
else:
turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))
Skip(-radius - 20)
else:
turtle.dot(5)
Skip(-radius)
turtle.right(6) def Week(t):
week = ["Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"]
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 + minute / 60.0
secHand.setheading(6 * second)
minHand.setheading(6 * minute)
hurHand.setheading(30 * hour) turtle.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()
turtle.tracer(True) # 100ms后继续调用tick
turtle.ontimer(Tick, 100) def main():
# 打开/关闭龟动画,并为更新图纸设置延迟。
turtle.tracer(False)
Init()
SetupClock(160)
turtle.tracer(True)
Tick()
turtle.mainloop() if __name__ == "__main__":
main()
————————(我是分割线)————————
参考:
1. https://blog.csdn.net/weixin_41988628/article/details/80718153
备注:
初次编辑时间:2019年10月4日16:35:32
环境:Windows 7 / Python 3.7.2
*【Python】【demo实验30】【练习实例】【使用Turtle实现实时时钟效果】的更多相关文章
- Python 利用Python编写简单网络爬虫实例3
利用Python编写简单网络爬虫实例3 by:授客 QQ:1033553122 实验环境 python版本:3.3.5(2.7下报错 实验目的 获取目标网站“http://bbs.51testing. ...
- Python 利用Python编写简单网络爬虫实例2
利用Python编写简单网络爬虫实例2 by:授客 QQ:1033553122 实验环境 python版本:3.3.5(2.7下报错 实验目的 获取目标网站“http://www.51testing. ...
- python+requests接口自动化测试框架实例详解
python+requests接口自动化测试框架实例详解 转自https://my.oschina.net/u/3041656/blog/820023 摘要: python + requests实 ...
- Python namedtuple(命名元组)使用实例
Python namedtuple(命名元组)使用实例 #!/usr/bin/python3 import collections MyTupleClass = collections.namedtu ...
- Python中对列表排序实例
Python中对列表排序实例 发布时间:2015-01-04 09:01:50 投稿:junjie 这篇文章主要介绍了Python中对列表排序实例,本文给出了9个List的排序实例,需要的朋友可以参考 ...
- Python中类的继承代码实例
Python中类的继承代码实例 这篇文章主要介绍了Python中类的继承代码实例,本文直接给出代码及运行效果,需要的朋友可以参考下 相对于C 的继承编写,Python更简洁,而且效率也是很高的,下面编 ...
- Python程序设计实验报告三:分支结构程序设计
安徽工程大学 Python程序设计 实验报告 班级 物流191 姓名 姚彩琴 学号3190505129 成绩 日期 2020.4.5 指导老师 修宇 [实验目的 ...
- python迭代器与iter()函数实例教程
python迭代器与iter()函数实例教程 发布时间:2014-07-16编辑:脚本学堂 本文介绍了python迭代器与iter()函数的用法,Python 的迭代无缝地支持序列对象,而且它还允许程 ...
- python中的类和实例
今天花了两个多小时后搜索相关博客看了看python中有关类和实例的介绍,差不多大概明白了. python中的类和c++中的类是一样的,不同之处就是c++的类,如果含有成员变量,并且成员变量发生变化后, ...
随机推荐
- @Controller 文件相关 @RequestMapping @PostMapping @PutMapping @DeleteMapping @PatchMapping
https://blog.csdn.net/magi1201/article/details/82226289(copy) 最近学习看一些代码,发现对于发送请求这件事,有的地方用@RequestMap ...
- BZOJ 4562: [Haoi2016]食物链 拓扑排序
建反图,跑一个拓扑排序dp即可. Code: #include <bits/stdc++.h> #define ll long long #define N 100005 #define ...
- codeforces#1108E2. Array and Segments (线段树+扫描线)
题目链接: http://codeforces.com/contest/1108/problem/E2 题意: 给出$n$个数和$m$个操作 每个操作是下标为$l$到$r$的数减一 选出某些操作,使$ ...
- Light Switching(SPOJ LITE)—— 线段树成段更新异或值
题目连接:http://www.spoj.com/problems/LITE/en/. 题意:有若干个灯泡,每次对一段操作,这一段原先是亮的,就关了:原先是关着的,就打开.询问某一段的打开的灯泡的个数 ...
- python 二分查找代码
二分查找的条件是必须是排好的数字 """二分查找""" def binary_searhc(arr, target): n = len(ar ...
- Linux之profile、bash_profile、bashrc文件
来自: profile.bash_profile.bashrc文件的作用与区别 1. profile 文件 1.1 profile 文件的作用 profile(/etc/profile),用于设置系统 ...
- VS2008 Qt Designer 中自定义信号槽
一.Qt Designer自定义槽函数 发现:在VS2008 +Qt4.7 中打开ui文件,所用的英文QT Designer工具,没有转到槽函数的功能,不如QtCreator自带的QtDesigne ...
- python练习-(秒转时分秒,时分秒转秒)-对比linux中文件的上次更改时间跟当前时间相差多久。
具体代码如下> import paramiko,re,datetime,time ssh=paramiko.SSHClient() ssh.set_missing_host_key_policy ...
- Flume-事务与传输流程
一.Flume 事务 流程图 Put 事务流程 doPut:将批数据先写入临时缓冲区 putList doCommit:检查 channel 内存队列是否足够合并 doRollback:channel ...
- 性能分析 | MySQL 的慢查分析实例
最近遇见一个 MySQL 的慢查问题,于是排查了下,这里把相关的过程做个总结. 定位原因 我首先查看了 MySQL 的慢查询日志,发现有这样一条 query 耗时非常长(大概在 1 秒多),而且扫描的 ...