Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mysterious "Attribute Errors" when Python GUI Programming

Status
Not open for further replies.

CoralSnake

Programmer
Mar 7, 2005
2
0
0
US
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.
 
Hello world doesn't have an attribute main, main is a function defined only in the __init__ method. Try unindenting your "def main" to the correct level, then run it.
 
Also, doing a "dir( win )" shows that it has a 'pack_propagate' and a 'pack_slaves', but not a 'pack'. Perhaps your tutorial is out of date with the latest version of Tk. Or maybe you're just doing it wrong, I don't do Tk, so I'm speaking solely from the standpoint of pure python.
 
Thanks. I also asked about this on comp.language.python on Usenet and this is pretty much the same information they gave me there as regarding the GTK example. I am especially interested in Python and Euphoria for programming my software
because I can place it under the GPL as source alone and it will still run without compilation but can still be compiled to byte code for proprietary software. (I basically do both.)

I also think that with a good GUI library like GTK, QT or WxWidgets it would be a good competitor with Java and Mono/.NET
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top