Code: Alles auswählen
from Tkinter import *
import thread
import cmath
import time
class TkAnalogClock(Canvas):
_grad = 0.1047
def __init__(self,master,size=100,clockbg='white',timeshift=0,show_second=1,**kw):
Canvas.__init__(self,master,width=size,height=size,**kw)
self.show_second = show_second
self.clockbg = clockbg
self.timeshift = timeshift
self.size = size
self.__setup()
def __setup(self):
size = self.size
c = size/2
s = c/10
mthick = size/50
hthick = size/40
dot_min = 1
dot_max = dot_min + 4
ldot_min = dot_max + c/10
self.center = [c,c]
self.create_oval(1,1,size-1,size-1,fill=self.clockbg)
self.create_oval(c-hthick,c-hthick,c+hthick,c+hthick,fill='black')
n = [(c-1,dot_min),(c-1,dot_max),(c+1,dot_max),(c+1,dot_min)]
nl = [(c-1,ldot_min),(c-1,dot_min),(c+1,dot_min),(c+1,ldot_min)]
for i in range(60):
if i%5 == 0:
self.create_polygon(nl)
self.__rotate(self._grad*i,nl,None)
else:
if size > 99:
self.create_polygon(n)
self.__rotate(self._grad*i,n,None)
self.coords = {
"second": [(c-1,c),(c-1,c-size*0.4),(c+1,c-size*0.4),(c+1,c)],
"minute": [(c-mthick,c),(c,c-size*0.4),(c+mthick,c)],
"hour" : [(c-hthick,c),(c,c-size*0.25),(c+hthick,c)]
}
if self.show_second:
self.create_polygon(self.coords["second"],tags="second")
self.create_polygon(self.coords["minute"],tags="minute")
self.create_polygon(self.coords["hour"],tags="hour")
h,m,s = time.localtime()[3:6]
h = h+self.timeshift
self.__rotate(self._grad*(h%12*5+m/12),self.coords["hour"],"hour")
self.__rotate(self._grad*m,self.coords["minute"],"minute")
if self.show_second:
self.__rotate(self._grad*s,self.coords["second"],"second")
thread.start_new_thread(self.__tick, (h,m,s))
def __rotate(self,direction,coordinates,tag):
angle = cmath.exp(direction*1j)
offset = complex(self.center[0], self.center[1])
newxy = []
for x,y in coordinates:
v = angle * (complex(x,y) - offset) + offset
newxy.append((v.real,v.imag))
self.delete(tag)
self.create_polygon(newxy,tags=tag)
if tag:
self.coords[tag] = newxy
self.update()
def __tick(self,h,m,s):
while 1:
s += 1
if s % 60 == 0:
s = 0
m += 1
self.__rotate(self._grad,self.coords["minute"],"minute")
if m % 12 == 0:
h += 1
self.__rotate(self._grad,self.coords["hour"],"hour")
if self.show_second:
self.__rotate(self._grad,self.coords["second"],"second")
time.sleep(1)
if __name__ == '__main__':
root = Tk()
'''
f = Frame()
f.pack(side=LEFT)
Label(f,text="London").grid(row=0,column=0)
Label(f,text="Paris").grid(row=0,column=1)
Label(f,text="NewYork").grid(row=2,column=0)
Label(f,text="Peking").grid(row=2,column=1)
TkAnalogClock(f,size=100,timeshift=-1).grid(row=1,column=0)
TkAnalogClock(f,size=90,timeshift=-2,show_second=0).grid(row=1,column=1)
TkAnalogClock(f,size=70,timeshift=1).grid(row=3,column=0)
TkAnalogClock(f,size=50,timeshift=2,show_second=0).grid(row=3,column=1)
TkAnalogClock(root,size=200,clockbg="red").pack(side=RIGHT)
'''
TkAnalogClock(root,size=500).pack()
root.mainloop()