Another pieclock, check the other one here: http://tkinter.unpy.net/wiki/PieClock

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

pieclock2.png


CategoryGames

tkinter: PieClock%2 (last edited 2011-03-07 11:18:28 by GuilhermePolo)