Timing in MacPython?

Peter Stoehr peter.stoehr at sdm.de
Wed Dec 15 09:11:00 EST 1999


Hi Michael,

maybe you can take one or two ideas from my WebTime-Program. It's
attached to this article.

Greetings from munich
    Peter

ventrego at yahoo.com schrieb:
> 
> I'm trying to create a simple timer application - something like a
> kitchen timer, just computerized.  However, I'm running into several
> problems:
> 
> First,can I force TKinter update the window?  I've set up a label and a
> button with TKinter textvariables, but the changes don't happen until
> after my program returns to idle - in this case, after the time interval
> has elapsed.  I'd like to have changes updated while the timer's waiting,
> so that the user knows everything's working correctly.
> 
> Second, is there an easy way to sound the system alert sound?   I'm
> referring the one that people can choose in the Sound/Monitors & Sound
> control panel.  It's possible to create a waveform and play it a la' the
> morse.py demo program, but this seems excessively inelegant.
> 
> Finally, is there a non-blocking time-delay command?  I'm using
> time.sleep(1), which is working well, but it stops all other processes
> from executing.  Ideally, I'd like this timer to run in the background!
> If I was on a UNIX system, I'd use signals, which would be VERY easy,
> but I'm a Mac addict at heart. :)
> 
> Thanks for any help -
> Michael

-- 
Dr. Peter Stoehr 		mailto:peter.stoehr at sdm.de
sd&m AG                 http://www.sdm.de
software design & management 
Thomas-Dehler-Strasse 27, D-81737 Muenchen, Germany
Tel ++49 89 63812-783, Fax -444
-------------- next part --------------
#
#    Internet-Time for Python/tk
#   =============================
#
#   Author: Peter Stoehr
#           Teisenbergweg 6
#           85435 Erding
#           Germany
#
#           peter at peter-stoehr.de
#

import tkFont
import Tkinter
from time import *

oldBeat = 0

class InternetStandardTime:
    def __init__(self):
        self.msec_per_day  = 24 * 60 * 60 * 1000
        self.msec_per_beat = self.msec_per_day / 1000
        
    def getBeat(self):
        msec = self._getSecond() * 1000 
        the_beat = msec / self.msec_per_beat
        return int(the_beat)
    
    def msecToNextBeat(self):
        msec = self._getSecond() * 1000
        retValue = msec % int(self.msec_per_beat)
        return (int(self.msec_per_beat - retValue))
        
        
    def _getSecond(self):
        (year, month, day,
         hour, minute, second,
         day, jday, swz) = localtime(time())
        return(second + 60 * (minute + 60 * (hour - swz)))

        

def my_after():
    global window
    global ist
    global oldBeat
    
    theBeat = ist.getBeat()
    if (theBeat == oldBeat):
        window.after(25, my_after)
    else :
        oldBeat = theBeat
        beat_string = "Beat-Time: @ %3d" % (theBeat)
        window.configure(text=beat_string)
        window.update_idletasks()
        window.after(ist.msecToNextBeat(), my_after)

    
#
# Create the TK-interface
#
ist = InternetStandardTime()
root = Tkinter.Tk()
root.wm_transient()
    
window = Tkinter.Label(root,text="Hello World")
window.pack()
window.after(1000, my_after)

Tkinter.mainloop()


More information about the Python-list mailing list