前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 简易时钟

python 简易时钟

原创
作者头像
织幻妖
发布2022-12-21 13:31:07
7080
发布2022-12-21 13:31:07
举报

1.导入模块

代码语言:javascript
复制
'''导入模块'''
import turtle
import datetime

turtle模块:它可以让你使用海龟图形(turtle graphics)绘制图像

datatime模块重新封装了time模块,提供更多接口,提供的类有:date,time,datetime,timedelta,tzinfo。

2.设计时钟

代码语言:javascript
复制
'''悬空移动'''
def move(distance):
   turtle.penup()
   turtle.forward(distance)
   turtle.pendown()


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


'''创建时钟'''
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 getWeekday(today):
   return ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日'][today.weekday()]


'''获得今天的日期'''
def getDate(today):
   return '%s年%s月%s日' % (today.year, today.month, today.day)


'''动态显示表针'''
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)

3.运行时钟

代码语言:javascript
复制
'''开始运行时钟'''
def start():
   # 不显示绘制时钟的过程
   turtle.tracer(False)
   turtle.mode('logo')
   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)
   # 用于打印日期等文字
   printer = turtle.Turtle()
   printer.hideturtle()
   printer.penup()
   createClock(160)
   # 开始显示轨迹
   turtle.tracer(True)
   startTick(second_hand, minute_hand, hour_hand, printer)
   turtle.mainloop()


if __name__ == '__main__':
   start()

4.展示效果

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.导入模块
    • turtle模块:它可以让你使用海龟图形(turtle graphics)绘制图像
    • 2.设计时钟
    • 3.运行时钟
    • 4.展示效果
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档