Mysterious "Attribute Errors" when GUI Programming

Coral Snake coralsnake2 at yahoo.com
Wed Mar 9 02:25:11 EST 2005


klappnase wrote:
> "Coral Snake" <coralsnake2 at yahoo.com> wrote in message
news:<1110263501.198651.111150 at f14g2000cwb.googlegroups.com>...
>
> > ----------------------------------------------------------
> > Tkinter:
> >
> > from Tkinter import *
> > root = Tk()
>
> This creates the application's main window. The Tk() command is not
> some kind of initialization routine, but actually returns a ready to
> use toplevel widget.
>
> > win = Toplevel(root)
>
> This creates a child window with the parent "root";
>
> > win.pack()
>
> here you try to put the child window into the main window; this
cannot
> work,
> because a Tk() or Toplevel() window cannot contain other Toplevel()
> instances.
> Toplevel() is used for things like dialogs. If you need a separate
> container
> widget inside "root" use Frame() instead.
>
> > Label(win, text= "Hello, Python World").pack(side=TOP)
> > Button(win, text= "Close", command=win.quit).pack(side=RIGHT)
> > win.mainloop()
> > ---------------------------------------------------------
> >
> > AttributeError: Toplevel instance has no attribute 'pack'
> >
> > ---------------------------------------------------------
>
> The correct usage of what you tried looks like this:
>
> from Tkinter import *
> root = Tk()
> Label(win, text= "Hello, Python World").pack(side=TOP)
> Button(win, text= "Close", command=win.quit).pack(side=RIGHT)
> root.mainloop()
>
> I hope this helps
>
> Michael

Thank you all. It appears that I was right in my original opinion of
source code from the Python Developer's Handbook by Andre Lessa and the
PyGTK Tutorial. That is where these source codes came from.

The code in Question came from the Chapter Getting Started in the PyGTK
Tutorial and from Chapter 15, page 579 in the Python Developer's
Handbook.




More information about the Python-list mailing list