1 #coding=gbk
2 #-*- encoding:utf-8 -*-
3
4 importtime,datetime5 importmath6 importitertools7 importtkinter8 importthreading9
10 defget_clock_step(base_pntr,long_pntr):11 pos =[]12 for i in range(60):13 pos.append((base_pntr[0]+long_pntr*math.cos(i*math.pi/30),14 base_pntr[0]+long_pntr*math.sin(i*math.pi/30)))15 return pos[45:] + pos[:45]16
17 defgen_i_pos(c_pntr,long_pntr):18 for i,p inenumerate(get_clock_step(c_pntr,long_pntr)):19 yieldi,p20
21 classPointer:22
23 def __init__(self,c_pntr,long_pntr,cvns,scale=None,super_pntr=None,width=1,fill='black'):24 #参数说明:
25 #c_pntr: 表盘的中心坐标;
26 #long_pntr: 表针长;
27 #cvns: 画布引用;
28 #scale: 指针行走比例;
29 #super_pntr: 上级指针;
30 #width: 指针粗细;
31 #fill: 指针颜色;
32 self.pos_iter =itertools.cycle(gen_i_pos(c_pntr,long_pntr))33 self.scale =scale34 self.cvns =cvns35 self.crrnt_pos = self.pos_iter.__next__()[1]36 self.c_pntr =c_pntr37 self.super_pntr =super_pntr38 self.id_pntr =None39 self.width =width40 self.fill =fill41
42 defdraw(self):43 self.id_pntr = cvns.create_line(self.c_pntr,self.crrnt_pos,width=self.width,fill=self.fill)44
45 defwalk_step(self):46 self.delete()47 self.draw()48 i,self.crrnt_pos = self.pos_iter.__next__()49 if self.super_pntr and self.scale and (i + 1) % self.scale ==0:50 self.super_pntr.walk_step()51
52 defdelete(self):53 ifself.id_pntr:54 self.cvns.delete(self.id_pntr)55
56 defset_start(self,steps):57 for i in range(steps-1):58 self.pos_iter.__next__()59 self.crrnt_pos = self.pos_iter.__next__()[1]60
61 classPlateImg:62
63 def __init__(self,c_pntr,clock_r,cvns,img):64 self.cvns =cvns65 self.clock_r =clock_r66 self.c_pntr =c_pntr67 self.img =img68 self.pid =None69 self.draw()70
71 defdraw(self):72 self.im = tkinter.PhotoImage(file=self.img)73 self.pid = self.cvns.create_image(self.c_pntr,image =self.im)74
75 defdelete(self):76 ifself.pid:77 self.cvns.delete(self.pid)78
79 defset_img(self,img):80 self.img =img81 self.delete()82 self.draw()83
84
85
86 classInImg(PlateImg):87 defdraw(self):88 x =self.c_pntr[0]89 y = self.c_pntr[0] + self.clock_r / 5
90 self.im = tkinter.PhotoImage(file=self.img)91 self.pid = self.cvns.create_image(x,y,image =self.im)92
93 classCustomPlate:94 def __init__(self,c_pntr,clock_r,cvns,imgp='platex.gif',imgi='flowersx.gif'):95 self.pi =PlateImg(c_pntr,clock_r,cvns,imgp)96 self.ii =InImg(c_pntr,clock_r,cvns,imgi)97
98 defset_img(self,imgp,imgi):99 self.pi.set_img(imgp)100 self.ii.set_img(imgi)101
102 defdelete(self):103 self.pi.delete()104 self.ii.delete()105
106
107 classPlate:108
109 def __init__(self,c_pntr,clock_r,cvns,long=10):110 self.pos_a = get_clock_step(c_pntr,clock_r -long)111 self.pos_b =get_clock_step(c_pntr,clock_r)112 self.cvns =cvns113 self.plates =[]114 self.draw()115
116 defdraw(self):117 for i,p inenumerate(zip(self.pos_a,self.pos_b)):118 width = 5 if (i + 1) % 5 == 0 else 3
119 pid = self.cvns.create_line(*p,width=width)120 self.plates.append(pid)121
122 defdelete(self):123 ifself.plates:124 for item inself.plates:125 self.cvns.delete(item)126
127 classMyClock:128
129 def __init__(self,base_pntr,clock_r,cvns):130 self.c_pntr =base_pntr131 self.clock_r =clock_r132 self.cvns =cvns133 self.plate =Plate(base_pntr,clock_r,self.cvns)134 h,m,s =self.start_time()135 self.h_pntr = Pointer(base_pntr,3 * clock_r // 5,self.cvns,width=5,fill='blue')136 self.m_pntr = Pointer(base_pntr,4 * clock_r // 5,self.cvns,12,super_pntr=self.h_pntr,width=3,fill='green')137 self.h_pntr.set_start(h * 5)138 self.m_pntr.set_start(m)139 self.h_pntr.walk_step()140 self.m_pntr.walk_step()141 self.s_pntr = Pointer(base_pntr,clock_r-5,self.cvns,60,super_pntr=self.m_pntr,fill='red')142 self.s_pntr.set_start(s)143
144 defchg_plate(self):145 self.plate.delete()146 ifisinstance(self.plate,CustomPlate):147 self.plate =Plate(self.c_pntr,self.clock_r,self.cvns)148 else:149 self.plate =CustomPlate(self.c_pntr,self.clock_r,self.cvns)150 self.h_pntr.delete()151 self.h_pntr.draw()152 self.m_pntr.delete()153 self.m_pntr.draw()154
155 defset_img(self,imgp,imgi):156 if notisinstance(self.plate,CustomPlate):157 self.chg_plate()158 self.plate.set_img(imgp,imgi)159
160 defwalk_step(self):161 self.s_pntr.walk_step()162
163 defstart_time(self):164 crrnt_time =datetime.datetime.now()165 hour =crrnt_time.hour166 minute =crrnt_time.minute167 second =crrnt_time.second168 returnhour,minute,second169
170 classMyButton:171 def __init__(self,root,clock):172 self.clock =clock173 button = tkinter.Button(root,text = '改变表盘',command =self.chg_plate)174 button.pack()175
176 defchg_plate(self):177 self.clock.chg_plate()178
179 classMyTk(tkinter.Tk):180 defquit(self):181 StartClock.looping =False182 self.quit()183
184 classStartClock:185 looping =True186 def __init__(self,mc,root):187 self.mc =mc188 self.root =root189
190 defstart_clock(self):191 whileStartClock.looping:192 self.mc.walk_step()193 self.root.update()194 time.sleep(0.05)195
196 if __name__ == '__main__':197 root =MyTk()198 cvns = tkinter.Canvas(root,width=400,height=400,bg='white')199 cvns.pack()200 mc = MyClock((200,200),200,cvns)201 bt =MyButton(root,mc)202 t = threading.Thread(target=StartClock(mc,root).start_clock)203 t.setDaemon(True)204 t.start()205 root.resizable(False, False)206 root.mainloop()