苟有恒何必三更眠五更起     最无益莫过一日曝十日寒

tkinter-clock实例

模仿着前辈的脚步,画了个临时的时钟显示:

代码如下:

 1 # coding:utf-8
 2 
 3 from tkinter import *
 4 
 5 import math,time
 6 
 7 global List
 8 global i
 9 root = Tk()
10 List = []
11 root.title("a simple clock")
12 #设置窗口是否可以变化长/宽
13 root.resizable(1, 1)
14 def points():
15     for i in range(1,13):
16         x = 200 + 130*math.sin(2*math.pi*i/12)
17 
18         y = 200 - 130*math.cos(2*math.pi*i/12)
19 
20         canvas.create_text(x,y,text=i)
21 def createline(radius,line_width,rad):
22     x = 200 + radius * math.sin(rad)
23 
24     y = 200 - radius * math.cos(rad)
25 
26     i = canvas.create_line(200, 200, x, y, width=line_width)
27 
28     List.append(i)
29 canvas = Canvas(root, width=400, height=500, bd=0, highlightthickness=0)
30 canvas.pack()
31 #生成外圆
32 canvas.create_oval(50, 50, 350, 350)
33 #生成数字
34 points()
35 
36 while 1:
37 
38     tm=time.localtime()
39 
40     #cur_time=time.asctime(tm)
41     cur_time2 = time.strftime('%Y-%m-%d %X', time.localtime())
42 
43     t_hour=0
44 
45     if tm.tm_hour<=12:
46 
47         t_hour=tm.tm_hour
48 
49     else:
50 
51         t_hour=tm.tm_hour-12
52 
53     rad1=2*math.pi*(t_hour+tm.tm_min/60)/12
54 
55     rad2=2*math.pi*(tm.tm_min+tm.tm_sec/60)/60
56 
57     rad3=2*math.pi*tm.tm_sec/60
58 
59     createline(50,6,rad1)
60 
61     createline(90,3,rad2)
62 
63     createline(120,1,rad3)
64 
65     time_text=canvas.create_text(200,450,text=cur_time2)
66 
67     root.update()
68 
69     time.sleep(1)
70 
71     for item in List:
72         canvas.delete(item)
73     canvas.delete(time_text)
74 
75     #root.update()
76 
77 mainloop()
View Code

仅供参考!

 

posted @ 2018-01-17 14:56  月魄  阅读(389)  评论(0编辑  收藏  举报