Logo Search packages:      
Sourcecode: pythoncard version File versions

worldclock.py

#!/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()

Generated by  Doxygen 1.6.0   Back to index