Wednesday, January 18, 2012

Blag's Word Clock


Sabes lo que es un Word Clock? Bueno, es basicamente un reloj que te da la hora utilizando palabras en vez de numeros.


La primera vez que vi uno...sabia que queria comprarlo, pero luego pense y me dije a mi mismo..."Hey! Soy un programador...porque comprar uno cuando yo puedo hacerlo?" Ademas...me di cuenta de que la mayoria de los Word Clocks no te dan la hora exacta, como pueden ver en la imagen...asi que decidi arreglar eso -:)

El siguiente paso era decidir que lenguaje de programacion utilizar...necesitaba una buena interface grafica asi que ABAP y R estaban totalmente descontados...no estaba buscado hacer una version web, asi que PHP fue descartado tambien...asi que mi siguiente opcion fue un lenguaje que me encanta y que no quiero olvidarme tan rapidamente -;) Asi que elegi Python y wxPython para hacerlo funcionar -:) Desde luego...estoy lejos de ser un experto en Python, asi que estoy seguro de que mi codigo podria ser un poco mas corto -:P





#Blag's Word Clock
#Alvaro "Blag" Tejada Galindo
#17/01/2012
import wx
import time
import datetime

class MyApp(wx.App):
def OnInit(self):
self.frame = MyFrame(None, title="Blag's Word Clock")
self.SetTopWindow(self.frame)
self.frame.Show()
return True

class MyFrame(wx.Frame):
def __init__(self, parent, id=wx.ID_ANY, title="",
pos=wx.DefaultPosition, size=(330, 260),
style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER,
name="MyFrame"):
super(MyFrame, self).__init__(parent, id, title, pos,size, style, name)
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.update, self.timer)
self.timer.Start(1000)
self.SetBackgroundColour("Black")
text_font = wx.Font(15, wx.SWISS, wx.NORMAL, wx.NORMAL, False, u'Arial')
self.label_it=wx.StaticText(self,1,"IT",wx.Point(10,10))
self.label_it.SetForegroundColour("yellow")
self.label_it.SetFont(text_font)
self.label_is=wx.StaticText(self,1,"IS",wx.Point(35,10))
self.label_is.SetForegroundColour("yellow")
self.label_is.SetFont(text_font)
self.label_half=wx.StaticText(self,1,"HALF",wx.Point(60,10))
self.label_half.SetForegroundColour("gray")
self.label_half.SetFont(text_font)
self.label_twenty=wx.StaticText(self,1,"TWENTY",wx.Point(120,10))
self.label_twenty.SetForegroundColour("gray")
self.label_twenty.SetFont(text_font)
self.label_quarter=wx.StaticText(self,1,"QUARTER",wx.Point(215,10))
self.label_quarter.SetForegroundColour("gray")
self.label_quarter.SetFont(text_font)

self.label_ten=wx.StaticText(self,1,"TEN",wx.Point(10,40))
self.label_ten.SetForegroundColour("gray")
self.label_ten.SetFont(text_font)
self.label_to=wx.StaticText(self,1,"TO",wx.Point(75,40))
self.label_to.SetForegroundColour("gray")
self.label_to.SetFont(text_font)
self.label_one=wx.StaticText(self,1,"ONE",wx.Point(120,40))
self.label_one.SetForegroundColour("gray")
self.label_one.SetFont(text_font)
self.label_two=wx.StaticText(self,1,"TWO",wx.Point(180,40))
self.label_two.SetForegroundColour("gray")
self.label_two.SetFont(text_font)
self.label_three=wx.StaticText(self,1,"THREE",wx.Point(245,40))
self.label_three.SetForegroundColour("gray")
self.label_three.SetFont(text_font)

self.label_four=wx.StaticText(self,1,"FOUR",wx.Point(10,70))
self.label_four.SetForegroundColour("gray")
self.label_four.SetFont(text_font)
self.label_five=wx.StaticText(self,1,"FIVE",wx.Point(75,70))
self.label_five.SetForegroundColour("gray")
self.label_five.SetFont(text_font)
self.label_six=wx.StaticText(self,1,"SIX",wx.Point(125,70))
self.label_six.SetForegroundColour("gray")
self.label_six.SetFont(text_font)
self.label_seven=wx.StaticText(self,1,"SEVEN",wx.Point(170,70))
self.label_seven.SetForegroundColour("gray")
self.label_seven.SetFont(text_font)
self.label_eight=wx.StaticText(self,1,"EIGHT",wx.Point(250,70))
self.label_eight.SetForegroundColour("gray")
self.label_eight.SetFont(text_font)

self.label_nine=wx.StaticText(self,1,"NINE",wx.Point(10,100))
self.label_nine.SetForegroundColour("gray")
self.label_nine.SetFont(text_font)
self.label_eleven=wx.StaticText(self,1,"ELEVEN",wx.Point(110,100))
self.label_eleven.SetForegroundColour("gray")
self.label_eleven.SetFont(text_font)
self.label_twelve=wx.StaticText(self,1,"TWELVE",wx.Point(230,100))
self.label_twelve.SetForegroundColour("gray")
self.label_twelve.SetFont(text_font)

self.label_minutes=wx.StaticText(self,1,"MINUTES",wx.Point(10,130))
self.label_minutes.SetForegroundColour("gray")
self.label_minutes.SetFont(text_font)
self.label_past=wx.StaticText(self,1,"PAST",wx.Point(115,130))
self.label_past.SetForegroundColour("gray")
self.label_past.SetFont(text_font)
self.label_to_x=wx.StaticText(self,1,"TO",wx.Point(180,130))
self.label_to_x.SetForegroundColour("gray")
self.label_to_x.SetFont(text_font)
self.label_one_x=wx.StaticText(self,1,"ONE",wx.Point(215,130))
self.label_one_x.SetForegroundColour("gray")
self.label_one_x.SetFont(text_font)
self.label_two_x=wx.StaticText(self,1,"TWO",wx.Point(265,130))
self.label_two_x.SetForegroundColour("gray")
self.label_two_x.SetFont(text_font)

self.label_three_x=wx.StaticText(self,1,"THREE",wx.Point(10,160))
self.label_three_x.SetForegroundColour("gray")
self.label_three_x.SetFont(text_font)
self.label_four_x=wx.StaticText(self,1,"FOUR",wx.Point(85,160))
self.label_four_x.SetForegroundColour("gray")
self.label_four_x.SetFont(text_font)
self.label_five_x=wx.StaticText(self,1,"FIVE",wx.Point(150,160))
self.label_five_x.SetForegroundColour("gray")
self.label_five_x.SetFont(text_font)
self.label_six_x=wx.StaticText(self,1,"SIX",wx.Point(205,160))
self.label_six_x.SetForegroundColour("gray")
self.label_six_x.SetFont(text_font)
self.label_seven_x=wx.StaticText(self,1,"SEVEN",wx.Point(245,160))
self.label_seven_x.SetForegroundColour("gray")
self.label_seven_x.SetFont(text_font)

self.label_eight_x=wx.StaticText(self,1,"EIGHT",wx.Point(10,190))
self.label_eight_x.SetForegroundColour("gray")
self.label_eight_x.SetFont(text_font)
self.label_nine_x=wx.StaticText(self,1,"NINE",wx.Point(95,190))
self.label_nine_x.SetForegroundColour("gray")
self.label_nine_x.SetFont(text_font)
self.label_ten_x=wx.StaticText(self,1,"TEN",wx.Point(165,190))
self.label_ten_x.SetForegroundColour("gray")
self.label_ten_x.SetFont(text_font)
self.label_eleven_x=wx.StaticText(self,1,"ELEVEN",wx.Point(235,190))
self.label_eleven_x.SetForegroundColour("gray")
self.label_eleven_x.SetFont(text_font)

self.label_twelve_x=wx.StaticText(self,1,"TWELVE",wx.Point(10,220))
self.label_twelve_x.SetForegroundColour("gray")
self.label_twelve_x.SetFont(text_font)
self.label_oclock=wx.StaticText(self,1,"O'CLOCK",wx.Point(125,220))
self.label_oclock.SetForegroundColour("gray")
self.label_oclock.SetFont(text_font)
self.label_am=wx.StaticText(self,1,"AM",wx.Point(235,220))
self.label_am.SetForegroundColour("gray")
self.label_am.SetFont(text_font)
self.label_pm=wx.StaticText(self,1,"PM",wx.Point(283,220))
self.label_pm.SetForegroundColour("gray")
self.label_pm.SetFont(text_font)

def reset_colours(self):
self.label_pm.SetForegroundColour("gray")
self.label_am.SetForegroundColour("gray")
self.label_oclock.SetForegroundColour("gray")
self.label_one.SetForegroundColour("gray")
self.label_two.SetForegroundColour("gray")
self.label_three.SetForegroundColour("gray")
self.label_four.SetForegroundColour("gray")
self.label_five.SetForegroundColour("gray")
self.label_six.SetForegroundColour("gray")
self.label_seven.SetForegroundColour("gray")
self.label_eight.SetForegroundColour("gray")
self.label_nine.SetForegroundColour("gray")
self.label_one_x.SetForegroundColour("gray")
self.label_two_x.SetForegroundColour("gray")
self.label_three_x.SetForegroundColour("gray")
self.label_four_x.SetForegroundColour("gray")
self.label_five_x.SetForegroundColour("gray")
self.label_six_x.SetForegroundColour("gray")
self.label_seven_x.SetForegroundColour("gray")
self.label_eight_x.SetForegroundColour("gray")
self.label_nine_x.SetForegroundColour("gray")
self.label_half.SetForegroundColour("gray")
self.label_twenty.SetForegroundColour("gray")
self.label_quarter.SetForegroundColour("gray")
self.label_ten.SetForegroundColour("gray")
self.label_ten_x.SetForegroundColour("gray")
self.label_eleven.SetForegroundColour("gray")
self.label_eleven_x.SetForegroundColour("gray")
self.label_twelve.SetForegroundColour("gray")
self.label_twelve_x.SetForegroundColour("gray")

def hours(self,hour):
if hour == 12:
self.label_twelve.SetForegroundColour("yellow")
elif hour == 11:
self.label_eleven.SetForegroundColour("yellow")
elif hour == 10:
self.label_ten.SetForegroundColour("yellow")
elif hour == 9:
self.label_nine.SetForegroundColour("yellow")
elif hour == 8:
self.label_eight.SetForegroundColour("yellow")
elif hour == 7:
self.label_seven.SetForegroundColour("yellow")
elif hour == 6:
self.label_six.SetForegroundColour("yellow")
elif hour == 5:
self.label_five.SetForegroundColour("yellow")
elif hour == 4:
self.label_four.SetForegroundColour("yellow")
elif hour == 3:
self.label_three.SetForegroundColour("yellow")
elif hour == 2:
self.label_two.SetForegroundColour("yellow")
elif hour == 1:
self.label_one.SetForegroundColour("yellow")

def minutes(self,reminder):
if reminder == 9:
self.label_nine.SetForegroundColour("yellow")
if reminder == 8:
self.label_eight.SetForegroundColour("yellow")
if reminder == 7:
self.label_seven.SetForegroundColour("yellow")
if reminder == 6:
self.label_six.SetForegroundColour("yellow")
if reminder == 5:
self.label_five.SetForegroundColour("yellow")
if reminder == 4:
self.label_four.SetForegroundColour("yellow")
if reminder == 3:
self.label_three.SetForegroundColour("yellow")
if reminder == 2:
self.label_two.SetForegroundColour("yellow")
if reminder == 1:
self.label_one.SetForegroundColour("yellow")

def update(self, event):
self.reset_colours()
now=datetime.datetime.now()
hour = now.hour
minute = now.minute
if hour >= 12 and minute != 0:
self.label_pm.SetForegroundColour("yellow")
hour = hour - 12
elif hour < 12 and minute != 0:
self.label_am.SetForegroundColour("yellow")
elif minute == 0:
self.label_oclock.SetForegroundColour("yellow")
if hour >= 12:
hour = hour - 12
self.hours(hour)
else:
self.hours(hour)

if minute >= 1 and minute <= 29:
self.label_minutes.SetForegroundColour("yellow")
self.label_past.SetForegroundColour("yellow")
if hour == 12:
self.label_twelve_x.SetForegroundColour("yellow")
elif hour == 11:
self.label_eleven_x.SetForegroundColour("yellow")
elif hour == 10:
self.label_ten_x.SetForegroundColour("yellow")
elif hour == 9:
self.label_nine_x.SetForegroundColour("yellow")
elif hour == 8:
self.label_eight_x.SetForegroundColour("yellow")
elif hour == 7:
self.label_seven_x.SetForegroundColour("yellow")
elif hour == 6:
self.label_six_x.SetForegroundColour("yellow")
elif hour == 5:
self.label_five_x.SetForegroundColour("yellow")
elif hour == 4:
self.label_four_x.SetForegroundColour("yellow")
elif hour == 3:
self.label_three_x.SetForegroundColour("yellow")
elif hour == 2:
self.label_two_x.SetForegroundColour("yellow")
elif hour == 1:
self.label_one_x.SetForegroundColour("yellow")
division = minute / 20
reminder = minute % 20
if division == 1 and reminder == 0:
self.label_twenty.SetForegroundColour("yellow")
elif division == 1 and reminder != 0:
self.label_twenty.SetForegroundColour("yellow")
self.minutes(reminder)
division = minute / 10
reminder = minute % 10
if division == 1 and reminder == 0:
self.label_ten.SetForegroundColour("yellow")
elif division == 1 and reminder != 0:
self.label_ten.SetForegroundColour("yellow")
if reminder == 9:
self.label_nine.SetForegroundColour("yellow")
if reminder == 8:
self.label_eight.SetForegroundColour("yellow")
if reminder == 7:
self.label_seven.SetForegroundColour("yellow")
if reminder == 6:
self.label_six.SetForegroundColour("yellow")
if reminder == 5:
self.label_ten.SetForegroundColour("gray")
self.label_quarter.SetForegroundColour("yellow")
if reminder == 4:
self.label_four.SetForegroundColour("yellow")
if reminder == 3:
self.label_three.SetForegroundColour("yellow")
if reminder == 2:
self.label_ten.SetForegroundColour("gray")
self.label_twelve.SetForegroundColour("yellow")
if reminder == 1:
self.label_ten.SetForegroundColour("gray")
self.label_eleven.SetForegroundColour("yellow")
elif division == 0 and reminder != 0:
if reminder == 9:
self.label_nine.SetForegroundColour("yellow")
if reminder == 8:
self.label_eight.SetForegroundColour("yellow")
if reminder == 7:
self.label_seven.SetForegroundColour("yellow")
if reminder == 6:
self.label_six.SetForegroundColour("yellow")
if reminder == 5:
self.label_five.SetForegroundColour("yellow")
if reminder == 4:
self.label_four.SetForegroundColour("yellow")
if reminder == 3:
self.label_three.SetForegroundColour("yellow")
if reminder == 2:
self.label_two.SetForegroundColour("yellow")
if reminder == 1:
self.label_one.SetForegroundColour("yellow")

elif minute == 30:
self.hours(hour)
self.label_half.SetForegroundColour("yellow")
self.label_past.SetForegroundColour("yellow")

elif minute >= 29 and minute <= 59:
self.label_minutes.SetForegroundColour("yellow")
minute = 60 - minute
self.label_to_x.SetForegroundColour("yellow")
hour = hour + 1
if hour == 12:
self.label_twelve_x.SetForegroundColour("yellow")
elif hour == 11:
self.label_eleven_x.SetForegroundColour("yellow")
elif hour == 10:
self.label_ten_x.SetForegroundColour("yellow")
elif hour == 9:
self.label_nine_x.SetForegroundColour("yellow")
elif hour == 8:
self.label_eight_x.SetForegroundColour("yellow")
elif hour == 7:
self.label_seven_x.SetForegroundColour("yellow")
elif hour == 6:
self.label_six_x.SetForegroundColour("yellow")
elif hour == 5:
self.label_five_x.SetForegroundColour("yellow")
elif hour == 4:
self.label_four_x.SetForegroundColour("yellow")
elif hour == 3:
self.label_three_x.SetForegroundColour("yellow")
elif hour == 2:
self.label_two_x.SetForegroundColour("yellow")
elif hour == 1:
self.label_one_x.SetForegroundColour("yellow")
division = minute / 20
reminder = minute % 20
if division == 1 and reminder == 0:
self.label_twenty.SetForegroundColour("yellow")
elif division == 1 and reminder != 0:
self.label_twenty.SetForegroundColour("yellow")
if reminder == 9:
self.label_nine.SetForegroundColour("yellow")
if reminder == 8:
self.label_eight.SetForegroundColour("yellow")
if reminder == 7:
self.label_seven.SetForegroundColour("yellow")
if reminder == 6:
self.label_six.SetForegroundColour("yellow")
if reminder == 5:
self.label_five.SetForegroundColour("yellow")
if reminder == 4:
self.label_four.SetForegroundColour("yellow")
if reminder == 3:
self.label_three.SetForegroundColour("yellow")
if reminder == 2:
self.label_two.SetForegroundColour("yellow")
if reminder == 1:
self.label_one.SetForegroundColour("yellow")
division = minute / 10
reminder = minute % 10
if division == 1 and reminder == 0:
self.label_ten.SetForegroundColour("yellow")
elif division == 1 and reminder != 0:
self.label_ten.SetForegroundColour("yellow")
if reminder == 9:
self.label_nine.SetForegroundColour("yellow")
if reminder == 8:
self.label_eight.SetForegroundColour("yellow")
if reminder == 7:
self.label_seven.SetForegroundColour("yellow")
if reminder == 6:
self.label_six.SetForegroundColour("yellow")
if reminder == 5:
self.label_ten.SetForegroundColour("gray")
self.label_quarter.SetForegroundColour("yellow")
if reminder == 4:
self.label_four.SetForegroundColour("yellow")
if reminder == 3:
self.label_three.SetForegroundColour("yellow")
if reminder == 2:
self.label_ten.SetForegroundColour("gray")
self.label_twelve.SetForegroundColour("yellow")
if reminder == 1:
self.label_ten.SetForegroundColour("gray")
self.label_eleven.SetForegroundColour("yellow")
elif division == 0 and reminder != 0:
self.minutes(reminder)

if __name__ == "__main__":
app = MyApp(False)
app.MainLoop()

Saludos,

Blag.

No comments: