[Tutor] methods within a class
Alan Colburn
aicolburn@yahoo.com
Tue, 15 Oct 2002 13:32:49 -0700
Hi all--
Can you help me distinguish between these two simple programs? They differ
only in terms of statements I highlighted via commenting. [If indentation
doesn't look right on your screen, just ignore...]
This one works fine:
****************************************************************************
*****
from Tkinter import *
root=Tk()
class Program(Frame):
def __init__(self):
Frame.__init__(self)
self.pack()
self.entry1=Entry(self)
self.entry1.bind('<Key-Return>', self.gettext)
self.entry1.pack(side=TOP, fill=X)
# self.printText()
def gettext(self, event):
global text
text=self.entry1.get()
self.printText()
def printText(self):
print "The value of the variable text is: ", text
if __name__=="__main__":
app=Program()
app.mainloop()
****************************************************************************
*****
On the other hand, this one does not:
****************************************************************************
*****
from Tkinter import *
root=Tk()
class Program(Frame):
def __init__(self):
Frame.__init__(self)
self.pack()
self.entry1=Entry(self)
self.entry1.bind('<Key-Return>', self.gettext)
self.entry1.pack(side=TOP, fill=X)
self.printText()
def gettext(self, event):
global text
text=self.entry1.get()
# self.printText()
def printText(self):
print "The value of the variable text is: ", text
if __name__=="__main__":
app=Program()
app.mainloop()
I know the answer will be relatively simple; I just need a little lesson on
methods and classes I guess.
Thanks, as always, for your help. -- Alan C.
p.s. Scot (in Berlin) -- you may recognize parts of this small program from
a response you were kind enough to send me last summer :-)