Mysterious "Attribute Errors" when GUI Programming
Steve Holden
steve at holdenweb.com
Tue Mar 8 07:30:14 EST 2005
Coral Snake wrote:
> I am having problems with programming even simple "Hello World"
> programs from books and tutorials that use Python GUI libraries. Such
> Programs cause python to throw "Attribute Errors" even when the
> "attributes" being asked for by the errors exist in the source code.
> This has happened to me in both the standard python GUI Library Tkinter
> and in pyGTK here are the codes for the "Hello World Programs involved
> and their corosponding "Attribute Errors":
>
> ----------------------------------------------------------
> Tkinter:
>
> from Tkinter import *
> root = Tk()
> win = Toplevel(root)
> win.pack()
> 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'
>
> ---------------------------------------------------------
> pyGTK
>
> import pygtk
> pygtk.require('2.0')
> import gtk
>
> class HelloWorld:
> def hello(self, widget, data=None):
> print "Hello World"
>
> def delete_event(self, widget, event, data= None):
> print "delete event occured"
> return gtk.FALSE
>
> def destroy(self, widget, data = None):
> gtk.main_quit()
>
> def __init__(self):
> self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
> self.window.connect("delete_event", self.delete_event)
> self.window.connect("destroy", self.destroy)
> self.window.set_border_width(10)
> self.button = gtk.Button("Hello, World!")
> self.button.connect("clicked", self.hello, None)
> self.button.connect_object("clicked",
> gtk.Widget.destroy, self.window)
> self.window.add(self.button)
> self.button.show()
> self.window.show()
>
> def main(self):
> gtk.main()
>
> if __name__ == "__main__":
> hello = HelloWorld()
> hello.main()
>
> ------------------------------------------------------------
>
> AttributeError: HelloWorld instance has no attribute 'main'
>
> ------------------------------------------------------------
> As you can see if you look at this code the "attributes"
> being asked for by both programs exist in the source code but python
> insists that they DON'T. What I want to know is what kind of bugs
> either in my source code or in Python itself leads it to to throw these
> "Attribute Errors" when the "attribute" being asked for by the error
> exists in the source code.
>
There's absolutely no point trying do divine how to write Tkinter-based
programs by reading the source, though it's a brave approach. But ...
>>> from Tkinter import *
>>> root = Tk()
>>> win = Toplevel(root)
>>> "pack" in dir(win)
False
>>>
tells you, absolutely beyond a shadow of a doubt, that Toplevel windows
don't have a "pack" method.
Take a look at a few of the working examples of Tkinter programs, that
should tell you what you are doing wrong.
regards
Steve
More information about the Python-list
mailing list