GUI Program Error

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

    #1

    GUI Program Error

    Hi, I'm using the "Learning to Program" GUI tutorial on

    and am trying to write my first GUI. However, the code the tutorial
    gives for starting by making a window:


    import Tkinter

    top = Tkinter.Tk()

    dir(top)

    Does not work. The Python interpreter does not complain about anything,
    but the window the tutorial promises will apperar will not. I'm using
    Python 2.4.2 and the Eric 3.7.0 IDE on Ubuntu 5.10. Anybody know what
    to do?

    -- /usr/bin/byte

  • John Salerno

    #2
    Re: GUI Program Error

    Byte wrote:[color=blue]
    > Hi, I'm using the "Learning to Program" GUI tutorial on
    > http://www.freenetpages.co.uk/hp/alan.gauld/
    > and am trying to write my first GUI. However, the code the tutorial
    > gives for starting by making a window:
    >
    >
    > import Tkinter
    >
    > top = Tkinter.Tk()
    >
    > dir(top)
    >
    > Does not work. The Python interpreter does not complain about anything,
    > but the window the tutorial promises will apperar will not. I'm using
    > Python 2.4.2 and the Eric 3.7.0 IDE on Ubuntu 5.10. Anybody know what
    > to do?[/color]

    You have to call top.mainloop() for it work. What is dir(), btw? Is it a
    class for creating the application?

    Comment

    • John Salerno

      #3
      Re: GUI Program Error

      John Salerno wrote:
      [color=blue]
      > What is dir(), btw? Is it a
      > class for creating the application?[/color]

      Heh heh, how quickly I forget about built-ins. :) Try something like this:

      import Tkinter as tk

      root = tk.Tk()
      label = tk.Label(root, text='Hello world!')
      label.pack()
      root.mainloop()

      Comment

      • Bernard Lebel

        #4
        Re: GUI Program Error

        On 6/5/06, John Salerno <johnjsal@nospa mgmail.com> wrote:[color=blue]
        > What is dir(), btw? Is it a class for creating the application?[/color]


        [Bernard] In your Python documentation, dir() is described in the
        built-in functions (section 2.1) as well as the tutorial, in the
        "Modules" section (chapter 6). Unless you were being sarcastic? ;-)


        Bernard

        Comment

        • John Salerno

          #5
          Re: GUI Program Error

          Bernard Lebel wrote:
          [color=blue]
          > Unless you were being sarcastic? ;-)[/color]

          Just temporarily dense. :)

          Comment

          • Byte

            #6
            Re: GUI Program Error

            Thanks, this works. Now, since the tutorial I was using is clearly
            rubbish, any better ones? For a (relative) newbie please. Also, how do
            I code a GUI Hello World program?

            -- /usr/bin/byte

            Comment

            • John Salerno

              #7
              Re: GUI Program Error

              Byte wrote:[color=blue]
              > Thanks, this works. Now, since the tutorial I was using is clearly
              > rubbish, any better ones? For a (relative) newbie please. Also, how do
              > I code a GUI Hello World program?
              >
              > -- /usr/bin/byte
              >[/color]

              Actually, that site is generally considered to be a pretty good place to
              start for tutorials. I think maybe you didn't go far enough through the
              "Tour of Some Common Widgets" section before you experienced problems
              (i.e., you didn't get to pack() or mainloop(), etc.).

              But I will agree with you that it is confusing when it says, following
              the top = Tk() line, "Notice that a new blank window has appeared",
              because as you noticed, nothing appears yet. My only guess is that this
              is either a mistake, or the tutorial is a little old, because I think
              Tkinter used to behave differently in the interactive prompt than it
              does now.

              Anyhow, I would stick with that site, but you might also try Fredrik's
              tutorial: http://www.pythonware.com/library/tkinter/introduction/

              It's a great little intro, but also a reference to the widgets, methods,
              etc. I pretty much learned everything so far from that site (admittedly
              I'm just as new as you are, but it's still a good site!) :)

              Comment

              • John Salerno

                #8
                Re: GUI Program Error

                Byte wrote:
                [color=blue]
                > Also, how do
                > I code a GUI Hello World program?[/color]

                P.S. The link I supplied begins with two versions of a Hello World app.

                Comment

                • jmdeschamps@gmail.com

                  #9
                  Re: GUI Program Error


                  Byte wrote:[color=blue]
                  > Thanks, this works. Now, since the tutorial I was using is clearly
                  > rubbish, any better ones? For a (relative) newbie please. Also, how do
                  > I code a GUI Hello World program?
                  >
                  > -- /usr/bin/byte[/color]

                  How does John Salerno previous code NOT fit the bill?
                  [color=blue][color=green]
                  >> John Salerno wrote:
                  >> ...
                  >> import Tkinter as tk
                  >>
                  >> root = tk.Tk()
                  >> label = tk.Label(root, text='Hello world!')
                  >> label.pack()
                  >> root.mainloop()
                  >> ...[/color][/color]

                  I think you should get a copy of "an-introduction-to-tkinter" from
                  Fredrik Lundh and try out the different usage examples and experiment,
                  then post more specific questions (after searching this newsgroup since
                  many questions lie here already answered).

                  Comment

                  • Eric Brunel

                    #10
                    Re: GUI Program Error

                    On Wed, 07 Jun 2006 17:38:39 GMT, John Salerno <johnjsal@NOSPA Mgmail.com>
                    wrote:
                    [snip][color=blue]
                    > But I will agree with you that it is confusing when it says, following
                    > the top = Tk() line, "Notice that a new blank window has appeared",
                    > because as you noticed, nothing appears yet. My only guess is that this
                    > is either a mistake, or the tutorial is a little old, because I think
                    > Tkinter used to behave differently in the interactive prompt than it
                    > does now.[/color]

                    It may be a platform-specific issue: On Unix/Linux, a window *does* appear
                    when you instantiate Tk, at least with tcl/tk 8.3 and 8.4 (which is the
                    latest stable version AFAIK).
                    --
                    python -c "print ''.join([chr(154 - ord(c)) for c in
                    'U(17zX(%,5.zmz 5(17l8(%,5.Z*(9 3-965$l7+-'])"

                    Comment

                    • Fredrik Lundh

                      #11
                      Re: GUI Program Error

                      Eric Brunel wrote:
                      [color=blue]
                      > It may be a platform-specific issue: On Unix/Linux, a window *does* appear
                      > when you instantiate Tk, at least with tcl/tk 8.3 and 8.4 (which is the
                      > latest stable version AFAIK).[/color]

                      same on Windows, but it often appears *beneath* the window you're typing
                      into, so you have to look for it in the task bar.

                      this also relies on Python being able to keep the Tkinter event loop
                      going behind the scenes; different command-line implementations have
                      different limitations.

                      for example, on Windows, the event loop runs only when you're at an
                      empty Python prompt; as soon as you start typing the next command, the
                      interpreter switches over to "text input mode", and the event loop is
                      paused until you press return.

                      </F>

                      Comment

                      • John Salerno

                        #12
                        Re: GUI Program Error

                        Fredrik Lundh wrote:[color=blue]
                        > Eric Brunel wrote:
                        >[color=green]
                        >> It may be a platform-specific issue: On Unix/Linux, a window *does*
                        >> appear when you instantiate Tk, at least with tcl/tk 8.3 and 8.4
                        >> (which is the latest stable version AFAIK).[/color]
                        >
                        > same on Windows, but it often appears *beneath* the window you're typing
                        > into, so you have to look for it in the task bar.
                        >
                        > this also relies on Python being able to keep the Tkinter event loop
                        > going behind the scenes; different command-line implementations have
                        > different limitations.
                        >
                        > for example, on Windows, the event loop runs only when you're at an
                        > empty Python prompt; as soon as you start typing the next command, the
                        > interpreter switches over to "text input mode", and the event loop is
                        > paused until you press return.
                        >
                        > </F>
                        >[/color]

                        I thought it might be something like that. A window doesn't appear for
                        me at all, but it may be because I haven't updated Tkinter since
                        installing Python, so maybe it's a little older.

                        Comment

                        • Scott David Daniels

                          #13
                          Re: GUI Program Error

                          John Salerno wrote:[color=blue]
                          > <Next from Fredrik Lundh>[color=green]
                          >> <Quoting Eric Brunel>[color=darkred]
                          >>> It may be a platform-specific issue: On Unix/Linux, a window *does*
                          >>> appear when you instantiate Tk, at least with tcl/tk 8.3 and 8.4
                          >>> (which is the latest stable version AFAIK).[/color][/color][/color]

                          Here you should have chimed in with your OS and Python versions.
                          [color=blue][color=green]
                          >> same on Windows, but it often appears *beneath* the window you're
                          >> typing into, so you have to look for it in the task bar.
                          >>
                          >> this also relies on Python being able to keep the Tkinter event loop
                          >> going behind the scenes; different command-line implementations have
                          >> different limitations.
                          >>
                          >> for example, on Windows, the event loop runs only when you're at an
                          >> empty Python prompt; as soon as you start typing the next command, the
                          >> interpreter switches over to "text input mode", and the event loop is
                          >> paused until you press return.[/color]
                          >
                          > I thought it might be something like that. A window doesn't appear for
                          > me at all, but it may be because I haven't updated Tkinter since
                          > installing Python, so maybe it's a little older.[/color]

                          When running on Windows 2000 (at least), with Python 2.3 or greater,
                          Idle now (by default) stays more separate from the process running the
                          user's code. As a result, debugging and namespaces are more stable, but
                          the users space doesn't have a display loop running. To help out in
                          cases like this, Idle has the "-n" switch which means roughly "no
                          subprocess." If you are on windows (I have no idea what system you are
                          running on), you can create a shortcut like I have (I call it Py24One).
                          I just copied the Idle shortcut, and changed the "target"
                          from:
                          C:\Python24\pyt honw.exe C:\Python24\Lib \idlelib\idle.p yw
                          to:
                          C:\Python24\pyt honw.exe C:\Python24\Lib \idlelib\idle.p yw -n

                          That "target", by the way, can also be typed directly to the command
                          line to start Idle in "no subprocess" mode.

                          The big advantage to using Idle this way is that (because Idle is
                          already running a display loop), you can see the effects of your
                          commands immediately. So the comments in your intro will hold true.

                          --Scott David Daniels
                          scott.daniels@a cm.org

                          Comment

                          Working...