Nub needs help withTkinter
__peter__ at web.de
Tue Dec 9 21:29:09 CET 2003
> So, I'm stuck again. I tried putting the code into a class, might not
> 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.beatNum = Label(self, text = "Beat Counter : ")
> self.beatNum.config(text = "Beat Counter : %d" %beats)
> 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()
The Tkinter toolkit has problems of its own, so you better learn the basics
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
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 *
def __init__(self, parent):
self.beatContainer = Frame(parent)
self.beats = 0
self.beatNum = Label(self.beatContainer)
self.beatNum.config(text="Beat Counter : %d" %self.beats)
def beatCounter(self, event):
print "increment counter"
self.beats = self.beats + 1
def beatReset(self, event):
print "reset counter"
self.beats = 0
root = Tk()
b = beatX(root)
Again, learning the language with a non-GUI script will probably be more
rewarding. Good luck!
More information about the Python-list