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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
#!/usr/bin/python
"""
KEA notes to myself
Created: 2001-07-22
__version__ = "$Revision: 1.27 $"
__date__ = "$Date: 2004/08/30 14:50:08 $"
__author__ = "Kevin Altis <altis@semi-retired.com>"
I'm using an ImageButton rather than an image because I want to catch
mouseMove (hmm, should that be mouseOver ?) events and pop up a relative
time for east coast, midwest, london, sydney, etc.
However, we need the option to set style=0 rather than the default
wxBU_AUTODRAW.
http://www.time.gov/images/xearths/night.jpg
"""
import urllib
import os
import time
import wx
from cStringIO import StringIO
from PythonCard import graphic, log, model, timer
#from PythonCard.log import Log
import xearth
class WorldClock(model.Background):
def on_initialize(self, event):
self.url = ''
# Enable logging
###self.log = Log.getInstance()
# self.log.enable()
# Normally we enable only ERROR and WARNING messages
###self.log.enableLevels( [ Log.ERROR, Log.WARNING, Log.DEBUG, Log.INFO ] )
# KEA 2002-05-27
# switched to timer events
self.clockTimer = timer.Timer(self.components.staticTextClock, -1)
self.clockTimer.start(1000) # 1 second
self.imageTimer = timer.Timer(self.components.imageButtonWorld, -1)
self.imageTimer.start(5 * 60 * 1000) # 5 minutes
# force the first update
self.updateDateAndTime()
self.updateImage()
def on_staticTextClock_timer(self, event):
self.updateDateAndTime()
def updateDateAndTime(self):
t = time.strftime("%I:%M %p")
if t[0] == "0":
t = t[1:]
if self.components.staticTextClock.text != t:
self.components.staticTextClock.text = t
d = time.strftime("%A, %B %d, %Y")
if self.components.staticTextDate.text != d:
self.components.staticTextDate.text = d
def on_imageButtonWorld_timer(self, event):
self.updateImage()
def updateImage(self):
log.info("interval is up...")
# try:
# jScript = "cscript //nologo xearth.js"
# file = os.popen(jScript)
# s = file.read()
# url = "http://www.time.gov/" + s
# except:
# url = "http://www.time.gov/images/xearths/night.jpg"
# #url = "http://www.time.gov/images/xearths/11N/100N.jpg"
url = "http://www.time.gov/" + xearth.getLatLong()
if url == self.url:
return
self.url = url
###self.log.info( "updating image ", url )
log.info("updating image ", url)
# download image from www.time.gov
try:
fp = urllib.urlopen(url)
jpg = fp.read()
fp.close()
except:
return
wImageButtonWorld = self.components.imageButtonWorld
# pre wxPython 2.3.3.1 the image data
# had to be written to disk first
#f = open('night2.jpg', "wb")
#f.write(jpg)
#f.close()
#newBitmap = graphic.Bitmap('night2.jpg')
# with wxPython 2.3.3.1 and above it is no longer
# necessary to write the file to disk before displaying it
newBitmap = graphic.Bitmap()
# I could probably use the fp file object, but using the
# jpg variable seems cleaner
newBitmap.setImageBits(wx.ImageFromStream(StringIO(jpg)))
wImageButtonWorld.bitmap = newBitmap
def on_close(self, event):
self.clockTimer.stop()
self.imageTimer.stop()
event.skip()
if __name__ == '__main__':
app = model.Application(WorldClock)
app.MainLoop()
|