Skip to content

Instantly share code, notes, and snippets.

@alvesjnr
Created September 20, 2012 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alvesjnr/3755658 to your computer and use it in GitHub Desktop.
Save alvesjnr/3755658 to your computer and use it in GitHub Desktop.
A simple clock using Tkinter
from time import strftime
import Tkinter
numbers = {0:'midnight',
1:'one',
2:'two',
3:'three',
4:'four',
5:'five',
6:'six',
7:'seven',
8:'eight',
9:'nine',
10:'ten',
11:'eleven',
12:'twelve',
15:'quarter',
20:'twenty',
25:'twenty and five',
30:'half'}
def get_hour(h):
if h > 12:
h -= 12
return numbers[h]
def get_minute(m):
if m in (58, 59, 0, 1, 2):
return ''
m = int((m + 2.5) / 5) * 5
if m <= 30:
prefix = 'past'
else:
m = 60 - m
prefix = 'to'
return "%s %s" % (numbers[m], prefix)
def get_time(h,m):
minute = get_minute(m)
if m >= 33:
h += 1
hour = get_hour(h)
if minute:
return "%s %s" % (minute, hour)
elif hour == 'midnight':
return hour
else:
return "%s o' clock" % get_hour(h)
class Clock(object):
def __init__(self, root):
self.clock = Tkinter.Label(master=root)
self.clock['font'] = 'Helvetica 44 bold'
self.clock.pack()
self._tictac()
def _tictac(self):
h,m = map(int, strftime("%H %M").split())
now = get_time(h,m)
if self.clock['text'] != now:
self.clock['text'] = now
self.clock.after(1000, self._tictac)
if __name__ == '__main__':
root = Tkinter.Tk()
c = Clock(root)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment