Mysterious "Attribute Errors" when GUI Programming

Robert Kern rkern at ucsd.edu
Tue Mar 8 01:50:18 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":

It might help us if you cited where these "Hello World" programs are 
coming from.

[snip]

>    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()

Are you sure about the indentation here? Because I'm willing to bet 
that's your problem in this example. I can't help with the Tkinter one, 
though.

> 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.

No, searching the source code for Tkinter shows no "Toplevel.pack" 
method (or in any of its base classes). Where is this program coming 
from? As for your GTK example, you have incorrect indentation.

> 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.

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter



More information about the Python-list mailing list