how to display clock?

Eric Brunel eric.brunel at N0SP4M.com
Mon Dec 8 04:30:13 EST 2003


Agency wrote:
> I'm still working on the bpm counter.
> 
> I need to have at least 2 displays that are not static. One would be a
> clock/running time and the other would should the current beat count.
> 
> How would I do this in Tkinter? I was thinkning of canvas-text, but
> there must be a widget for doing this kind of display. I'm a bit lost
> and would appreciate some help.

What's your exact problem here? Is it a problem with the display itself or the 
fact that it is not static? As for the display itself, a simple label should be 
enough if you use its textvariable option to make changes to its text easier. If 
the problem lies in the dynamicity, here is a simple example showing how to 
display two dynamic displays at the same time using only Python and Tkinter:

--clock-n-beat.py-------------------------
from Tkinter import *

## Main window
root = Tk()

## The tempo to use for the beat
tempo = 90

## Tkinter variables for clock and beat
clockVar = IntVar()
beatVar = IntVar()
## First beat in measure is 1
beatVar.set(1)

## This function will be called periodically to update the clock
def clockPulse():
   ## Increase clock value
   clockVar.set(1 + clockVar.get())
   ## Call again current function in 1000 micro-seconds = 1 second
   root.after(1000, clockPulse)

## This function will be called periodically to update the beat
def beatPulse():
   ## Increase beat
   beat = beatVar.get() + 1
   ## Assuming a 4/4 measure, wrap beat if it's too high
   if beat > 4:
     beatVar.set(1)
   else:
     beatVar.set(beat)
   ## Call again this function in the correct amount of micro-seconds
   ## depending on tempo
   root.after(int(60000.0 / tempo), beatPulse)

## Title and display for clock
Label(root, text="Clock:").grid(row=0, column=0, sticky=W)
Label(root, textvariable=clockVar).grid(row=0, column=1, sticky=W)

## Title and display for beat
Label(root, text="Beat:").grid(row=1, column=0, sticky=W)
Label(root, textvariable=beatVar).grid(row=1, column=1, sticky=W)

## Quit button
Button(root, command=root.quit, text='Quit').grid(row=2, column=0, columnspan=2)

## Make sure the first calls to the functions updating the clock and beat
## will be made
root.after(1000, clockPulse)
root.after(int(60000.0 / tempo), beatPulse)

## Go!
root.mainloop()
------------------------------------------

HTH
-- 
- Eric Brunel <eric dot brunel at pragmadev dot com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com





More information about the Python-list mailing list