Nub needs help withTkinter

A

Agency

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()
-------------------------------------------------------------------
 
E

Eric Brunel

Agency said:
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()
-------------------------------------------------------------------

I can't even begin to undeerstand what you're trying to do here:
<KeyPress-space> is only valid in a string as an event description that you can
pass to the various bind methods, so converting it to an int has no meaning at
all for me...

Maybe you should begin with a tutorial for Tkinter; you'd surely understand
better what's going on and how you can do things with it. "Thinking in Tkinter"
is usually a good place to start:
http://www.ferg.org/thinking_in_tkinter/index.html
(follow the link all_programs.html to get the full the whole series of scripts
and the explanations around them).

HTH
 
M

Martin Franklin

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
 
A

Agency

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
-------------------------------------------------------------------------
Wow. Thats perfect. I'm not sure what you meant about the global
statement, this is my first time trying to program anything, but I'll
look through the docs and see if I find something out. Now, I just
have figure out how this codes works and have it correspond to a clock
function on a display, which gives the bpm.

Thanks, I was getting rather frustrated. I'll post back if I get
stumped.
 
B

Bob van der Poel

Agency said:
-------------------------------------------------------------------------
Wow. Thats perfect. I'm not sure what you meant about the global
statement, this is my first time trying to program anything, but I'll
look through the docs and see if I find something out. Now, I just
have figure out how this codes works and have it correspond to a clock
function on a display, which gives the bpm.

Thanks, I was getting rather frustrated. I'll post back if I get
stumped.

Or look on my website for the countbeats program I wrote awhile ago. I
think this is what you're trying to do ... but no reason not to reinvent
the wheel if you want :)
 
A

Agency

O.K.

So, I'm stuck again. I tried putting the code into a class, might not
be
smart given what I know, but I did it. I'm thinking that the beat,
time, and bpm would each have their own class and display/label. I
know that the pack() determines if something shows up. So, I'm
guessing that somewhere along the line pack() is not be referenced
right. My problem is that I have a window,
with no label showing up. I also just noticed that there is no linkage
between the beatContainer/frame and the beatNum/label. Here is the
newbie code:

from Tkinter import *


class beatX:
def _init_(self, parent):
self.beatContainer = Frame(parent)
self.beatContainer.pack()

self.beatNum = Label(self, text = "Beat Counter : ")
self.beatNum.config(text = "Beat Counter : %d" %beats)
self.beatNum.pack()
self.beatNum.bind("<space>", self.beatCounter)
self.beatNum.bind("<Return>", self.beatReset)

def beatCounter(event):
print "increment counter"
beats = beats + 1

def beatReset(event):
print "reset counter"
beats = 0

root = Tk()
root.mainloop()
 
P

Peter Otten

Agency said:
O.K.

So, I'm stuck again. I tried putting the code into a class, might not
be
smart given what I know, but I did it. I'm thinking that the beat,
time, and bpm would each have their own class and display/label. I
know that the pack() determines if something shows up. So, I'm
guessing that somewhere along the line pack() is not be referenced
right. My problem is that I have a window,
with no label showing up. I also just noticed that there is no linkage
between the beatContainer/frame and the beatNum/label. Here is the
newbie code:

from Tkinter import *


class beatX:
def _init_(self, parent):
self.beatContainer = Frame(parent)
self.beatContainer.pack()

self.beatNum = Label(self, text = "Beat Counter : ")
self.beatNum.config(text = "Beat Counter : %d" %beats)
self.beatNum.pack()
self.beatNum.bind("<space>", self.beatCounter)
self.beatNum.bind("<Return>", self.beatReset)

def beatCounter(event):
print "increment counter"
beats = beats + 1

def beatReset(event):
print "reset counter"
beats = 0

root = Tk()
root.mainloop()

The Tkinter toolkit has problems of its own, so you better learn the basics
first:

The __init__() method has two preceding and two trailing underscores.
Every method in a class has self as the first parameter, unless you know why
it's otherwise.
The class definition is normally not sufficient, you will also want at least
one instance, created like so: instance = Class(args).

Now to your beatX class. I've made the minimum changes necessary to avoid a
traceback and ignorance of user input, and I recommend that you change it
incrementally to meet your needs, so that when - not if :) - it breaks
again you can go back to the previous state.
See the beats attribute for the simplest usage pattern of a class attribute
and how showCounter() is invoked in three different places to keep the
beatNum Label uptodate.

from Tkinter import *


class beatX:
def __init__(self, parent):
self.beatContainer = Frame(parent)
self.beatContainer.pack()
self.beats = 0
self.beatNum = Label(self.beatContainer)
self.beatNum.pack()
parent.bind("<space>", self.beatCounter)
parent.bind("<Return>", self.beatReset)
self.showCounter()

def showCounter(self):
self.beatNum.config(text="Beat Counter : %d" %self.beats)

def beatCounter(self, event):
print "increment counter"
self.beats = self.beats + 1
self.showCounter()

def beatReset(self, event):
print "reset counter"
self.beats = 0
self.showCounter()

root = Tk()
b = beatX(root)
root.mainloop()

Again, learning the language with a non-GUI script will probably be more
rewarding. Good luck!

Peter
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top