tkinter: making widgets instance or not?

John Salerno johnjsal at NOSPAMgmail.com
Tue Jun 6 15:00:22 EDT 2006


This is from the Tkinter tutorial:

from Tkinter import *

class App:

     def __init__(self, master):

         frame = Frame(master)
         frame.pack()

         self.button = Button(frame, text="QUIT", fg="red", 
command=frame.quit)
         self.button.pack(side=LEFT)

         self.hi_there = Button(frame, text="Hello", command=self.say_hi)
         self.hi_there.pack(side=LEFT)

     def say_hi(self):
         print "hi there, everyone!"

root = Tk()
app = App(root)
root.mainloop()

I'm wondering, why is frame created as a local variable, and the buttons 
as instance variables? What is the difference? Can you make frame an 
instance variable, or vice versa? (I tried it with the buttons and it 
seems 'self.' isn't necessary, but is this just good practice for larger 
programs?)



More information about the Python-list mailing list