Exiting Tkinter when using IDLE

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Batista, Facundo

    Exiting Tkinter when using IDLE

    I tested the following simple code:

    -----------------
    from Tkinter import *

    class App:

    def __init__(self, master):

    frame = Frame(master)
    frame.pack()

    self.button = Button(frame, text="QUIT", fg="red",
    command=frame.q uit)
    self.button.pac k(side=LEFT)

    self.hi_there = Button(frame, text="Hello", command=self.sa y_hi)
    self.hi_there.p ack(side=LEFT)

    def say_hi(self):
    print "hi there, everyone!"

    root = Tk()
    app = App(root)
    root.mainloop()
    -----------------

    I'm on Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)]
    on win32.

    When I double click the program, it works OK.

    When I execute it from IDLE, the first time I push the "QUIT" button, I just
    receive a second prompt in IDLE and nothing else happens. The second time I
    push the "QUIT" button, the program exits, but also exits IDLE!

    Do you know what's happening? With this behaviour it's difficult to develop
    using IDLE, :(

    .. Facundo

  • Jason Harper

    #2
    Re: Exiting Tkinter when using IDLE

    You have two issues here:

    1. Don't use the quit method, instead just close your window. Your QUIT
    button could perhaps use:
    command=self.de stroy

    2. Don't call mainloop() if IDLE already has one running. Try this:

    import sys
    if "idlelib" not in sys.modules:
    root.mainloop()

    Jason Harper

    Comment

    • Gerrit Muller

      #3
      Re: Exiting Tkinter when using IDLE

      Jason Harper wrote:
      [color=blue]
      > You have two issues here:
      >
      > 1. Don't use the quit method, instead just close your window. Your QUIT
      > button could perhaps use:
      > command=self.de stroy
      >
      > 2. Don't call mainloop() if IDLE already has one running. Try this:
      >
      > import sys
      > if "idlelib" not in sys.modules:
      > root.mainloop()
      >
      > Jason Harper[/color]

      I think that this answer deserves a place in a cookbook recipe. I did
      not work with Tkinter for more than a year, but I remember that I
      struggled with exactly the same issues and more or less experimentally
      reached this solution.

      thanks for the clarification!

      regards Gerrit

      --
      Gaudi systems architecting:


      Comment

      • Jason Harper

        #4
        Re: Exiting Tkinter when using IDLE

        An update to this advice I gave:
        [color=blue]
        > 1. Don't use the quit method, instead just close your window. Your QUIT
        > button could perhaps use:
        > command=self.de stroy
        >
        > 2. Don't call mainloop() if IDLE already has one running. Try this:
        >
        > import sys
        > if "idlelib" not in sys.modules:
        > root.mainloop()[/color]

        This only works (on Windows, at least) if IDLE is _not_ running user
        code in a subprocess. I've not found any combination of options that
        gets usable results (including the ability to introspect the program
        while running) when using a subprocess.

        Also, it seems to leave the process running forever if launched outside
        of IDLE, even after all the windows are closed. The simplest solution,
        assuming a single-window application, seems to be giving the main window
        a WM_DELETE_WINDO W protocol handler that does a self.quit(), but only if
        IDLE isn't running (determined as shown above).

        Here's a little inconsistency that had me really confused for a while:
        IDLE, when run from the Start menu (Win2K Pro, Python 2.3.3), uses a subprocess.
        IDLE, when run via "Open in IDLE" in a .py* file's right-click menu,
        does NOT use a subprocess.
        I haven't tracked down just where this difference is coming from.
        Jason Harper

        Comment

        • Eugene Van den Bulke

          #5
          Re: Exiting Tkinter when using IDLE

          On Fri, 12 Mar 2004 08:14:24 +0100, Gerrit Muller
          <gerrit.muller@ embeddedsystems .nl> wrote:
          [color=blue]
          > Jason Harper wrote:
          >[color=green]
          >> You have two issues here:
          >>
          >> 1. Don't use the quit method, instead just close your window. Your QUIT
          >> button could perhaps use:
          >> command=self.de stroy
          >>
          >> 2. Don't call mainloop() if IDLE already has one running. Try this:
          >>
          >> import sys
          >> if "idlelib" not in sys.modules:
          >> root.mainloop()
          >>
          >> Jason Harper[/color]
          >
          > I think that this answer deserves a place in a cookbook recipe. I did
          > not work with Tkinter for more than a year, but I remember that I
          > struggled with exactly the same issues and more or less experimentally
          > reached this solution.
          >
          > thanks for the clarification!
          >
          > regards Gerrit
          >[/color]

          I have a probleme with the "if idlelib" solution ... without the
          root.mainloop() the window does not appear after I pressed F5 to run the
          programm. But when I type root.mainloop() in the shell window that opens
          up, it works.

          Any idea? Thanks for your help.


          --
          Eugene Van den Bulke
          [-----



          -----]

          Comment

          • Jason Harper

            #6
            Re: Exiting Tkinter when using IDLE

            Eugene Van den Bulke wrote:[color=blue]
            > I have a probleme with the "if idlelib" solution ... without the
            > root.mainloop() the window does not appear after I pressed F5 to run the
            > programm. But when I type root.mainloop() in the shell window that opens
            > up, it works.[/color]

            That sounds like your IDLE is running your program in a subprocess,
            which means that it can't share IDLE's mainloop. See the followup I posted.

            In a subprocess, you can just have root.mainloop() at the end of your
            program unconditionally , but this prevents any communication back to the
            main IDLE process - your program works fine, but you can't inspect it.
            Jason Harper

            Comment

            • Kurt B. Kaiser

              #7
              Re: Exiting Tkinter when using IDLE

              Jason Harper <JasonHarper@po box.com> writes:
              [color=blue]
              > Here's a little inconsistency that had me really confused for a
              > while: IDLE, when run from the Start menu (Win2K Pro, Python 2.3.3),
              > uses a subprocess. IDLE, when run via "Open in IDLE" in a .py*
              > file's right-click menu, does NOT use a subprocess. I haven't
              > tracked down just where this difference is coming from.[/color]

              Right now, we only allow one copy of the IDLE subprocess, because the
              socket port is hard coded. The specification of the -n switch is in
              the Windows Explorer File Types assignment. We did that deliberately
              because people often click on several .py files to view them and that
              tried to start several copies of IDLE + subprocess, which failed.

              You can have as many copies of IDLE without the subprocess as you
              like, but they aren't nearly as useful for code development.

              The plan is to remove the hard coding of the port so several copies of
              IDLE + subprocess can run simultaneously. I am delaying that until I'm
              pretty sure that the subprocess is well behaved under all conditions.

              --
              KBK

              Comment

              Working...