how to display clock?

Discussion in 'Python' started by Agency, Dec 7, 2003.

  1. Agency

    Agency Guest

    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.
     
    Agency, Dec 7, 2003
    #1
    1. Advertising

  2. Agency

    Eric Brunel Guest

    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
     
    Eric Brunel, Dec 8, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Valentin Tihomirov

    Are clock and divided clock synchronous?

    Valentin Tihomirov, Oct 23, 2003, in forum: VHDL
    Replies:
    11
    Views:
    3,300
    louis lin
    Oct 28, 2003
  2. Replies:
    4
    Views:
    726
    Peter Alfke
    Apr 27, 2006
  3. Replies:
    5
    Views:
    2,176
    Ricardo
    Jun 23, 2006
  4. himassk
    Replies:
    1
    Views:
    1,236
    Paul Uiterlinden
    May 16, 2007
  5. pankaj.goel
    Replies:
    6
    Views:
    942
    pankaj.goel
    Nov 25, 2008
Loading...

Share This Page