Mysterious "Attribute Errors" when GUI Programming

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Coral Snake

    #1

    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.

  • Robert Kern

    #2
    Re: Mysterious "Attrib ute Errors" when GUI Programming

    Coral Snake wrote:[color=blue]
    > 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":[/color]

    It might help us if you cited where these "Hello World" programs are
    coming from.

    [snip]
    [color=blue]
    > 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()[/color]

    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.
    [color=blue]
    > 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.[/color]

    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.
    [color=blue]
    > 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.[/color]

    --
    Robert Kern
    rkern@ucsd.edu

    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter

    Comment

    • Steve Holden

      #3
      Re: Mysterious "Attrib ute Errors" when GUI Programming

      Coral Snake wrote:[color=blue]
      > 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.
      >[/color]

      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 ...
      [color=blue][color=green][color=darkred]
      >>> from Tkinter import *
      >>> root = Tk()
      >>> win = Toplevel(root)
      >>> "pack" in dir(win)[/color][/color][/color]
      False[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      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

      Comment

      • klappnase

        #4
        Re: Mysterious "Attrib ute Errors" when GUI Programming

        "Coral Snake" <coralsnake2@ya hoo.com> wrote in message news:<111026350 1.198651.111150 @f14g2000cwb.go oglegroups.com> ...
        [color=blue]
        > ----------------------------------------------------------
        > Tkinter:
        >
        > from Tkinter import *
        > root = Tk()[/color]

        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.
        [color=blue]
        > win = Toplevel(root)[/color]

        This creates a child window with the parent "root";
        [color=blue]
        > win.pack()[/color]

        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.
        [color=blue]
        > 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'
        >
        > ---------------------------------------------------------[/color]

        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

        Comment

        • Coral Snake

          #5
          Re: Mysterious &quot;Attrib ute Errors&quot; when GUI Programming

          klappnase wrote:[color=blue]
          > "Coral Snake" <coralsnake2@ya hoo.com> wrote in message[/color]
          news:<111026350 1.198651.111150 @f14g2000cwb.go oglegroups.com> ...[color=blue]
          >[color=green]
          > > ----------------------------------------------------------
          > > Tkinter:
          > >
          > > from Tkinter import *
          > > root = Tk()[/color]
          >
          > 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.
          >[color=green]
          > > win = Toplevel(root)[/color]
          >
          > This creates a child window with the parent "root";
          >[color=green]
          > > win.pack()[/color]
          >
          > here you try to put the child window into the main window; this[/color]
          cannot[color=blue]
          > 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.
          >[color=green]
          > > 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'
          > >
          > > ---------------------------------------------------------[/color]
          >
          > 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[/color]

          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.

          Comment

          • Robert Kern

            #6
            Re: Mysterious &quot;Attrib ute Errors&quot; when GUI Programming

            Coral Snake wrote:
            [color=blue]
            > 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[/color]

            Note that the code you wrote was incorrectly indented. The original
            code[1] was *correctly* indented and ought to work.
            [color=blue]
            > and from Chapter 15, page 579 in the Python Developer's
            > Handbook.[/color]

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

            [1]


            --
            Robert Kern
            rkern@ucsd.edu

            "In the fields of hell where the grass grows high
            Are the graves of dreams allowed to die."
            -- Richard Harter

            Comment

            • Coral Snake

              #7
              Re: Mysterious &quot;Attrib ute Errors&quot; when GUI Programming

              Again Thanks to everyone here. Both the GTK and the Tkinter example are
              running fine now.

              Comment

              Working...