473,394 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Mysterious "Attribute Errors" when GUI Programming

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.

Jul 18 '05 #1
6 3334
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
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
Jul 18 '05 #2
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

Jul 18 '05 #3
"Coral Snake" <co*********@yahoo.com> wrote in message news:<11**********************@f14g2000cwb.googleg roups.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
Jul 18 '05 #4
klappnase wrote:
"Coral Snake" <co*********@yahoo.com> wrote in message news:<11**********************@f14g2000cwb.googleg roups.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


Thank you all. It appears that I was right in my original opinion of
source code from the Python Developer's Handbook by Andre Lessa and the
PyGTK Tutorial. That is where these source codes came from.

The code in Question came from the Chapter Getting Started in the PyGTK
Tutorial and from Chapter 15, page 579 in the Python Developer's
Handbook.

Jul 18 '05 #5
Coral Snake wrote:
Thank you all. It appears that I was right in my original opinion of
source code from the Python Developer's Handbook by Andre Lessa and the
PyGTK Tutorial. That is where these source codes came from.

The code in Question came from the Chapter Getting Started in the PyGTK
Tutorial
Note that the code you wrote was incorrectly indented. The original
code[1] was *correctly* indented and ought to work.
and from Chapter 15, page 579 in the Python Developer's
Handbook.


I *have* heard that this book contains many errors.

[1]
http://www.moeraki.com/pygtktutorial...sec-HelloWorld

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
Jul 18 '05 #6
Again Thanks to everyone here. Both the GTK and the Tkinter example are
running fine now.

Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Torbjørn Pettersen | last post by:
I've got a table where I want some of the cells to use a background image. The cells have variable height, so I am using an image with a rather small height to fill up the background of the cells,...
24
by: Charles Crume | last post by:
Hello; My "index.htm" page has 3 frames (content, navigation bar, and logo). I set the "SRC" of the "logo" frame to a blank gif image and then want to change it's contents after the other two...
3
by: Pavils Jurjans | last post by:
Hello, I have bumped upon this problem: I do some client-side form processing with JavaScript, and for this I loop over all the forms in the document. In order to identify them, I read their...
3
by: Carl Lindmark | last post by:
*Cross-posting from microsoft.public.dotnet.languages.csharp, since I believe the question is better suited in this XML group* Hello all, I'm having some problems understanding all the ins and...
4
by: sam | last post by:
Hi all, When I use the HTML tidy tool in Firefox I see the following warning which I want to get rid of . I cleared all the errors and warnings it showed except for this one. I am not able to...
5
by: crystalattice | last post by:
I've finally figured out the basics of OOP; I've created a basic character creation class for my game and it works reasonably well. Now that I'm trying to build a subclass that has methods to...
0
by: =?Utf-8?B?QXR1bA==?= | last post by:
When .Net 1.0 webservice (VS2003) generates a wsdl - <wsdl:binding name="TestSoap" type="tns:TestSoap"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/(note:...
0
by: Gregory Gadow | last post by:
We have a number of development machines in our IT department, all running the same version of VS 2005 sp 1. Our company website and several compiled components were all written in VB.Net 2.0 using...
2
by: =?Utf-8?B?UmFscGggSQ==?= | last post by:
OK, Dell inspirion 9300 - 100 gb hd partitioned into 3 drives C: OS 10 gb D: Programs 20 gb E: Data 70 GB Page Files 0 on C: 4092 on D: 3070 on E:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.