2020-01-15

python实现实时时钟

导入库说明

  1. 导入turtle库。用于实现画时钟,时针以及日期时间的显示。
  2. 导入datatime库。用于读取系统本地日期时间。
  3. 导入time库。用于优化时钟,减少CPU资源占用。

模块说明

1.导入库以及设置背景的尺寸

import turtle as tt
import datetime as dt
import time

screen = tt.Screen()
screen.screensize(600, 600)
screen.tracer(0)

pen = tt.Turtle()
pen.hideturtle()
#title = screen.textinput("title", "please input your expected title name")
#screen.title(title)
screen.title("clock")

2.画时钟以及刻度

def draw_circle():
    pen.pencolor("pink")
    pen.up()
    pen.goto(0, -200)
    pen.seth(0)
    pen.down()
    pen.pensize(5)
    pen.circle(200)

    pen.up()
    pen.goto(0, 0)
    pen.seth(90)
	    for i in range(12):
        pen.fd(180)
        pen.down()
        pen.fd(20)
        pen.up()
        pen.goto(0, 0)
        pen.rt(6)
        for _ in range(4):
            pen.fd(190)
            pen.down()
            pen.fd(10)
            pen.up()
            pen.goto(0, 0)
            pen.rt(6)
def draw_trick():
    pen.up()
    pen.color("black")
    pen.goto(220, -10)
    pen.write("3", align="center", font=("Arial", 15, "normal"))
    pen.goto(0, 210)
    pen.write("0", align="center", font=("Arial", 15, "normal"))
    pen.goto(-220, -10)
    pen.write("9", align="center", font=("Arial", 15, "normal"))
    pen.goto(0, -235)
    pen.write("6", align="center", font=("Arial", 15, "normal"))

3.画时针以及箭头

def draw_clockwise(h, m, s):
    pen.up()
    pen.goto(0, 0)
    pen.pencolor("orange")
    pen.down()
    pen.pensize(7)
    pen.seth(90)
    pen.rt(h * 30 + m / 2)
    pen.fd(100)
    arrowhead("orange")

    pen.up()
    pen.goto(0, 0)
    pen.seth(90)
    pen.rt(m * 6 + s / 10)
    pen.pencolor("yellow")
    pen.down()
    pen.pensize(6)
    pen.fd(125)
    arrowhead("yellow")

	
    pen.up()
    pen.goto(0, 0)
    pen.seth(90)
    pen.rt(s * 6)
    pen.pencolor("green")
    pen.down()
    pen.pensize(5)
    pen.fd(150)
    arrowhead("green")

def arrowhead(color):
    pen.pensize(1)
    pen.up()
    pen.fd(10)
    pen.down()
    pen.color(color)
    pen.begin_fill()
    pen.left(150)
    pen.fd(20)
    pen.left(150)
    pen.fd(10)
    pen.right(60)
    pen.fd(10)
    pen.left(150)
    pen.fd(20)
    pen.end_fill()
    pen.right(30)

4.在时钟中显示日期时间
主要依赖于update()这个函数,才能实时更新,最后的time.sleep(0.98)是进行优化的代码(里面的参数小于1就好)。

while True:
    screen.update()
    now = dt.datetime.now()
    pen.clear()
    draw_circle()
    draw_clockwise(now.hour, now.minute, now.second)
    pen.up()
    pen.goto(0, 230)
    pen.color("blue")
    pen.write("%d:%02d:%02d" % (now.hour, now.minute, now.second), align="center", font=("Arial", 20, "normal"))
    pen.goto(0, -260)
    pen.write("今天是{}年{}月{}日".format(now.year, now.month, now.day), align="center", font=("Arial", 20, "normal"))
    draw_trick()
screen.mainloop()

5.结果显示
这个时钟有很多可以优化的地方,例如加上星期等等。

发布了1 篇原创文章 · 获赞 1 · 访问量 10
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览