| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
| import Tkinter
import time
def current_time():
return time.localtime()[3:6]
class PieClock(Tkinter.Canvas):
frequency = 250
def __init__(self, master=None, **kwargs):
Tkinter.Canvas.__init__(self, master, **kwargs)
self.bind('<Configure>', self.update_clock)
self.create_arcs()
self.running = True
def create_arcs(self):
self.create_oval(0, 0, 0, 0, width=2, tag='contour')
arc_angle = 360 / 3
self.create_arc(0, 0, 0, 0, start=90, extent=arc_angle, tag='hour',
fill='SteelBlue1', outline='')
self.create_arc(0, 0, 0, 0, start=90, extent=-arc_angle, tag='minute',
fill='SteelBlue3', outline='')
self.create_arc(0, 0, 0, 0, start=210, extent=arc_angle, tag='second',
fill='black', outline='')
def arcs_coords(self):
hour, minute, second = current_time()
hour %= 12
size = min(self.winfo_width(), self.winfo_height())
yield size
base = size / 2
sec_x = (base * second) / 60.
min_x = (base * minute) / 60.
hour_x = (base * hour) / 12.
for item in (hour_x, min_x, sec_x):
yield (base - item, base - item, base + item, base + item)
def update_clock(self, event=None):
coords = self.arcs_coords()
size = coords.next()
arc_hour, arc_min, arc_sec = coords
self.coords('contour', 2, 2, size - 2, size - 2)
self.coords('hour', *arc_hour)
self.coords('minute', *arc_min)
self.coords('second', *arc_sec)
def run(self):
self.update_clock()
if self.running:
self.after(self.frequency, self.run)
def stop(self):
self.running = False
def main():
root = Tkinter.Tk()
clock = PieClock(width=250, height=250)
clock.pack(expand=True, fill='both')
clock.run()
root.mainloop()
if __name__ == "__main__":
main() |