473,783 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(si de=TOP)
Button(win, text= "Close", command=win.qui t).pack(side=RI GHT)
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(se lf, 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.con nect("delete_ev ent", self.delete_eve nt)
self.window.con nect("destroy", self.destroy)
self.window.set _border_width(1 0)
self.button = gtk.Button("Hel lo, World!")
self.button.con nect("clicked", self.hello, None)
self.button.con nect_object("cl icked",
gtk.Widget.dest roy, self.window)
self.window.add (self.button)
self.button.sho w()
self.window.sho w()

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 3354
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.con nect("delete_ev ent", self.delete_eve nt)
self.window.con nect("destroy", self.destroy)
self.window.set _border_width(1 0)
self.button = gtk.Button("Hel lo, World!")
self.button.con nect("clicked", self.hello, None)
self.button.con nect_object("cl icked",
gtk.Widget.dest roy, self.window)
self.window.add (self.button)
self.button.sho w()
self.window.sho w()

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.p ack"
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(si de=TOP)
Button(win, text= "Close", command=win.qui t).pack(side=RI GHT)
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(se lf, 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.con nect("delete_ev ent", self.delete_eve nt)
self.window.con nect("destroy", self.destroy)
self.window.set _border_width(1 0)
self.button = gtk.Button("Hel lo, World!")
self.button.con nect("clicked", self.hello, None)
self.button.con nect_object("cl icked",
gtk.Widget.dest roy, self.window)
self.window.add (self.button)
self.button.sho w()
self.window.sho w()

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*********@ya hoo.com> wrote in message news:<11******* *************** @f14g2000cwb.go oglegroups.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(si de=TOP)
Button(win, text= "Close", command=win.qui t).pack(side=RI GHT)
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(si de=TOP)
Button(win, text= "Close", command=win.qui t).pack(side=RI GHT)
root.mainloop()

I hope this helps

Michael
Jul 18 '05 #4
klappnase wrote:
"Coral Snake" <co*********@ya hoo.com> wrote in message news:<11******* *************** @f14g2000cwb.go oglegroups.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(si de=TOP)
Button(win, text= "Close", command=win.qui t).pack(side=RI GHT)
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(si de=TOP)
Button(win, text= "Close", command=win.qui t).pack(side=RI GHT)
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
7264
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, thus making I look like one high image in there. When validating it, I get this error: there is no attribute "BACKGROUND" The validators also tell me there are no "HEIGHT" or
24
3531
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 frames have been loaded by using a javascript statement from the "navigation" frame, as shown below: top.window.ccs_logo.src = 'images/ccs_logo.gif'; alert(top.window.ccs_logo.src);
3
2707
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 "name" property (which sources from "name" HTML attribue). The problem is, that if the form contains form control named "name", it overwrites the form name property. In fact, I'm quite surprised that it's so easy to spoil any of the form object...
3
9396
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 outs with datasets and datatables (and navigating through the filled datatable)... Just when I thought I had gotten the hang of it, another problem arose: I can't seem to access the "xsi:type" attribute. That is, the XML file looks
4
3764
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 get rid of this, can someone help me find a solution. Warning: <iframe> proprietary attribute "onload" I have the code defined like this where it throws the warning. I have
5
3675
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 determine the rank of a character but I keep getting errors. I want to "redefine" some attributes from the base class so I can use them to help determine the rank. However, I get the error that my base class doesn't have the dictionary that...
0
2651
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: style attribute present) <wsdl:operation name="HelloWorld"> <soap:operation soapAction="http://tempuri.org/HelloWorld" style="document" /> But when .Net 2.0 webservice (VS2005) generates a wsdl -
0
1482
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 one of those machines, our "main dev." When I pull up the website in the VS IDE on that one machine, everything is fine. On any of the other machines, however, opening the website opens the site, then fills the Error List with just over a...
2
9514
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
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.