欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python实现简易动态时钟

程序员文章站 2023-11-20 09:14:58
本文实例为大家分享了python实现简易动态时钟的具体代码,供大家参考,具体内容如下 from turtle import * from datetime im...

本文实例为大家分享了python实现简易动态时钟的具体代码,供大家参考,具体内容如下

from turtle import *
from datetime import *
#移动到指定位置
def skip(step):
  penup()
  forward(step)
  pendown()
#画指针
def drawpointer(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 hrpointer, minpointer, secpointer, weektext
  #设置turtle logo模式,朝北
  mode("logo")
  drawpointer("hrpointer", 90)
  drawpointer("minpointer", 130)
  drawpointer("secpointer", 140)
  hrpointer = turtle()
  hrpointer.shape("hrpointer")
  minpointer = turtle()
  minpointer.shape("minpointer")
  secpointer = turtle()
  secpointer.shape("secpointer")
  secpointer.pencolor("red")
  for pointer in hrpointer, minpointer, secpointer:
    pointer.shapesize(3, 1, 1)
    pointer.speed(0)
  #文字输出
  weektext = turtle()
  weektext.hideturtle()
  weektext.penup()
#设置表盘形状
def setupclock(radius):
  reset()
#  clockpanel = turtle()
  pensize(7)
  pencolor("blue")
  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.0000001
  minute = t.minute + second/60.0
  hour = t.hour + minute/60.0
  secpointer.setheading(6*second)
  minpointer.setheading(6*minute)
  hrpointer.setheading(30*hour)
  tracer(false)
  weektext.forward(65)
  weektext.write(week(t), align="center",font=("courier", 14, "bold"))
  weektext.back(130)
  weektext.write(date(t), align="center",font=("courier", 14, "bold"))
  weektext.home()
  tracer(true)
  #间隔100ms调用一次
  ontimer(tick, 100)

def main():
  tracer(false)
  init()
  setupclock(160)
  tracer(true)
  tick()
  mainloop()
if __name__ == '__main__':
  main()

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