Mysterious "Attribute Errors" when GUI Programming

klappnase klappnase at web.de
Tue Mar 8 13:11:16 EST 2005


"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



More information about the Python-list mailing list