By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,183 Members | 1,190 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,183 IT Pros & Developers. It's quick & easy.

Exiting Tkinter when using IDLE

P: n/a
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.quit)
self.button.pack(side=LEFT)

self.hi_there = Button(frame, text="Hello", command=self.say_hi)
self.hi_there.pack(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

Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.destroy

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
Jul 18 '05 #2

P: n/a
Jason Harper wrote:
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.destroy

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


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:
http://www.extra.research.philips.com/natlab/sysarch/

Jul 18 '05 #3

P: n/a
An update to this advice I gave:
1. Don't use the quit method, instead just close your window. Your QUIT
button could perhaps use:
command=self.destroy

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

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


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_WINDOW 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
Jul 18 '05 #4

P: n/a
On Fri, 12 Mar 2004 08:14:24 +0100, Gerrit Muller
<ge***********@embeddedsystems.nl> wrote:
Jason Harper wrote:
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.destroy

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


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


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
[-----
www.boardkulture.com
www.actiphot.com
www.xsbar.com
-----]
Jul 18 '05 #5

P: n/a
Eugene Van den Bulke wrote:
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.


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
Jul 18 '05 #6

P: n/a
Jason Harper <Ja*********@pobox.com> writes:
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.


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
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.