473,396 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Nub needs help withTkinter

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()
-------------------------------------------------------------------
Jul 18 '05 #1
6 1856
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()
-------------------------------------------------------------------


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
--
- Eric Brunel <eric dot brunel at pragmadev dot com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

Jul 18 '05 #2
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 <mf********@gatwick.westerngeco.slb.com>
Jul 18 '05 #3
> 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.
Jul 18 '05 #4
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()
Jul 18 '05 #5
Agency wrote:
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
Jul 18 '05 #6


Agency wrote:
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.


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 :)

--
Bob van der Poel ** Wynndel, British Columbia, CANADA **
EMAIL: bv*****@kootenay.com
WWW: http://www.kootenay.com/~bvdpoel

Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Ron_Adam | last post by:
Hi, Thanks again for all the helping me understand the details of decorators. I put together a class to create decorators that could make them a lot easier to use. It still has a few glitches...
0
by: MDOPro | last post by:
A pal needs help on this site greetings fellow newsgroupers. I recently found a site called Microsoft Forums. In a very short time I became very known to the site and its an excellent tool....
19
by: David Logan | last post by:
We need an additional function in the String class. We need the ability to suppress empty fields, so that we can more effectively parse. Right now, multiple whitespace characters create multiple...
191
by: Xah Lee | last post by:
Software Needs Philosophers by Steve Yegge, 2006-04-15. Software needs philosophers. This thought has been nagging at me for a year now, and recently it's been growing like a tumor. One...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.