用Python模拟同步时钟
2019-01-28 18:04
330 查看
用Python模拟同步时钟
首先:导包
form turtle import form datetime import
其次
def Skip(step): penup() forward(step) pendown()
注册,建立表针Turtle
def mkHand(name, length): 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") 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) 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
输入星期
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 + minute/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)
判断加以输出
def main(): tracer(False) Init() SetupClock(160) tracer(True) Tick() mainloop() if __name__ == "__main__": main()
效果图如下
相关文章推荐
- Python实现模拟时钟代码推荐
- 模拟NTP协议实现时钟同步
- minecraft同步时钟-python
- Python实现模拟时钟代码推荐
- python模拟随机游走
- Python selenium模拟浏览器
- Python模拟HttpRequest的方法总结
- python get post模拟请求
- python 模拟登陆人人网,并发文字状态
- Python 模拟三级菜单
- python 针对selenium+phontomjs等模拟浏览器爬虫的反爬技术点
- Qt之模拟时钟
- 零基础学python-19.11 使用迭代工具模拟map
- Python笔记-IO同步和异步、 读写文件、StringIO和BytesIO
- 用Python模拟网站中对JavaScript加密需要的两种方法
- python3.5 模拟UDP客户端发送数据报文,报错:TypeError: a bytes-like object is required, not 'str'
- python简单模拟:把树存储在数据表中
- python线程中同步锁详解
- Physx Unity3D同步模拟
- python pyinotify 监控远程文件夹来实现即时全量同步