Newbie: question about Tkinter

Chad Netzer cnetzer at mail.arc.nasa.gov
Fri Feb 28 20:54:55 CET 2003


On Fri, 2003-02-28 at 09:33, Locash wrote:
> Hi,
> 
> I am a recreational programmer who has decided to give Python a try. 

> This is what I have:
> 
> ~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> from Tkinter import *
> 
> class Application(Frame):
>         
>     def __init__(self, master=None):
>         
>         Frame.__init__(self, master)
>         self.pack()
>         self.createWidgets()

Another poster (Howard) gave you a solution (it is a common question,
btw, regarding the Tkinter Image objects; you need to keep your own
references to them; the canvas doesn't automatically do this).

I just wanted to point out that it is a common mistake (I made it too,
when I started with Tkinter) to subclass a Frame to make your own
object, and then pack() that object in the constructor.

It is an attractive option, since it can make the usage code of your
subclassed object seem more clean.  But, consider that when you subclass
an object, you are guaranteeing (essentially) that the object IS A Frame
object.  And Frame objects do NOT get automatically packed.

It may not seem such a big deal (and for this specific case it likely is
not).  But keep it in mind as you use Tkinter more and more, since you
will eventually want to build reusable Frame-sublcasses, and you will
almost certainly not want to "auto-pack" them.  You can pack() as soon
as they are creted if you like:

Application().pack()

-- 
Bay Area Python Interest Group - http://www.baypiggies.net/

Chad Netzer
(any opinion expressed is my own and not NASA's or my employer's)







More information about the Python-list mailing list