Mysterious "Attribute Errors" when GUI Programming

Coral Snake coralsnake2 at yahoo.com
Tue Mar 8 01:31:41 EST 2005


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.




More information about the Python-list mailing list