Nub needs help withTkinter
Martin Franklin
mfranklin1 at gatwick.westerngeco.slb.com
Mon Dec 8 04:31:26 EST 2003
On Sat, 2003-12-06 at 23:23, Agency wrote:
> I'm trying to program a utility that counts the beats per minute in a
> song
> by tapping the spacebar. I already have a program that does this, but
> I wanted to make my own. The "enter" key resets the counter to zero
> along with one of two displays. I'm importing Tkinter and this is what
> I have so far. I get a syntax error at this spot ----->
> <KeyPress-space>
> Any thoughts how to get the keypress to increase the counter better?
> Is it possible to convert it to an int or something?
> ------------------------------------------------------------------
> from Tkinter import *
>
> root = Tk()
> w = Label(root, text="The BPM Counter")
> w.pack()
>
> class display1:
> def beat():
> beat = 0
> if beat > 10000:
> beat += <KeyPress-space>
>
> root.mainloop()
You will need to __bind__ a callback to the key press on a widget.
something like :-
from Tkinter import *
root=Tk()
lab = Label(root, text = "Beat Counter : ")
lab.pack()
beats = 0
def beatCounter(event):
print "increment counter"
global beats
beats = beats + 1
lab.config(text = "Beat Counter : %d" %beats)
def beatReset(event):
print "reset counter"
global beats
beats = 0
lab.config(text = "Beat Counter : %d" %beats)
root.bind("<space>", beatCounter)
root.bind("<Return >", beatReset)
root.mainloop()
You should rewrite the above so it does not use the global statement
('cause thats cheating!)
HTH
Martin
--
Martin Franklin <mfranklin1 at gatwick.westerngeco.slb.com>
More information about the Python-list
mailing list