Mysterious "Attribute Errors" when GUI Programming

Discussion in 'Python' started by Coral Snake, Mar 8, 2005.

  1. Coral Snake

    Coral Snake Guest

    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.
     
    Coral Snake, Mar 8, 2005
    #1
    1. Advertisements

  2. Coral Snake

    Robert Kern Guest

    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


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
     
    Robert Kern, Mar 8, 2005
    #2
    1. Advertisements

  3. Coral Snake

    Steve Holden Guest

    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
     
    Steve Holden, Mar 8, 2005
    #3
  4. Coral Snake

    klappnase Guest

    "Coral Snake" <> wrote in message news:<>...

    > ----------------------------------------------------------
    > 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
     
    klappnase, Mar 8, 2005
    #4
  5. Coral Snake

    Coral Snake Guest

    klappnase wrote:
    > "Coral Snake" <> wrote in message

    news:<>...
    >
    > > ----------------------------------------------------------
    > > 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.
     
    Coral Snake, Mar 9, 2005
    #5
  6. Coral Snake

    Robert Kern Guest

    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/pygtk2tutorial/ch-GettingStarted.html#sec-HelloWorld

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
     
    Robert Kern, Mar 9, 2005
    #6
  7. Coral Snake

    Coral Snake Guest

    Again Thanks to everyone here. Both the GTK and the Tkinter example are
    running fine now.
     
    Coral Snake, Mar 9, 2005
    #7
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. paul.foreman

    GUI - GUI value passing

    paul.foreman, Oct 22, 2004, in forum: Java
    Replies:
    5
    Views:
    1,062
    Michael Rauscher
    Oct 25, 2004
  2. ulysses
    Replies:
    4
    Views:
    1,082
    Werner Schiendl
    Oct 22, 2003
  3. Andrew Lapidas

    PyGTK GUI update without signals from GUI

    Andrew Lapidas, Apr 13, 2008, in forum: Python
    Replies:
    0
    Views:
    552
    Andrew Lapidas
    Apr 13, 2008
  4. Stefan Ram
    Replies:
    3
    Views:
    721
    Arne Vajhøj
    Nov 20, 2011
  5. Hal Fulton

    GUI and non-GUI data

    Hal Fulton, Aug 5, 2004, in forum: Ruby
    Replies:
    1
    Views:
    302
    Austin Ziegler
    Aug 5, 2004
  6. Flaab Mrlinux

    [Gui] Which GUI is available with Ruby?

    Flaab Mrlinux, Nov 15, 2006, in forum: Ruby
    Replies:
    3
    Views:
    234
    WoodHacker
    Nov 15, 2006
  7. Mark Summerfield
    Replies:
    0
    Views:
    386
    Mark Summerfield
    Jun 3, 2012
  8. Jai
    Replies:
    99
    Views:
    1,149
    Joshua Landau
    Dec 26, 2013
Loading...