python闹钟实例

# coding:utf-8

from tkinter import *

import math,time

global List
global i
root = Tk()
List = []
root.title("a simple clock")
#设置窗口是否可以变化长/宽
root.resizable(1, 1)
def points():
    for i in range(1,13):
        x = 200 + 130*math.sin(2*math.pi*i/12)

        y = 200 - 130*math.cos(2*math.pi*i/12)

        canvas.create_text(x,y,text=i)
def createline(radius,line_width,rad):
    x = 200 + radius * math.sin(rad)

    y = 200 - radius * math.cos(rad)

    i = canvas.create_line(200, 200, x, y, width=line_width)

    List.append(i)
canvas = Canvas(root, width=400, height=500, bd=0, highlightthickness=0)
canvas.pack()
#生成外圆
canvas.create_oval(50, 50, 350, 350)
#生成数字
points()

while 1:

    tm=time.localtime()

    #cur_time=time.asctime(tm)
    cur_time2 = time.strftime('%Y-%m-%d %X', time.localtime())

    t_hour=0

    if tm.tm_hour<=12:

        t_hour=tm.tm_hour

    else:

        t_hour=tm.tm_hour-12

    rad1=2*math.pi*(t_hour+tm.tm_min/60)/12

    rad2=2*math.pi*(tm.tm_min+tm.tm_sec/60)/60

    rad3=2*math.pi*tm.tm_sec/60

    createline(50,6,rad1)

    createline(90,3,rad2)

    createline(120,1,rad3)

    time_text=canvas.create_text(200,450,text=cur_time2)

    root.update()

    time.sleep(1)

    for item in List:
        canvas.delete(item)
    canvas.delete(time_text)

    #root.update()

mainloop()

  

posted @ 2024-03-27 17:32  飞雪飘鸿  阅读(11)  评论(0编辑  收藏  举报
https://damo.alibaba.com/ https://tianchi.aliyun.com/course?spm=5176.21206777.J_3941670930.5.87dc17c9BZNvLL