472,352 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,352 software developers and data experts.

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.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
6 3453
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andrei | last post by:
I've installed Python 2.3b2 on Windows XP (in "e:\programming\Python2.3"). I also have an older ActivePython 2.2.2 distro on my C-drive and Ruby...
3
by: srijit | last post by:
Hello, Any idea - why the following code crashes on my Win 98 machine with Python 2.3? Everytime I run this code, I have to reboot my machine. I...
1
by: John Hunter | last post by:
Does tkinter have an idle handler analogous to pygtk's gtk.idle_add? Normally I could find this myself on google, but googling for tkinter idle...
1
by: neutrinman | last post by:
How do I use Tkinter from IDLE? Tkinter can be used from IDLE attached to python 2.2, IDLE 0.8. But I couldn't use from IDLE attached to python...
4
by: Mike Tuller | last post by:
I recently purchased a book to learn python, and am at a part where I want to start working with GUIs. I have an OS X system, and am using the...
1
by: Jean Richelle | last post by:
Hello, I installed version 2.4.1 (and I tried also with 2.4.3) of Python under 10.3.9 (and 10.4.6), and I trying to use Tkinter. For simplicity...
6
by: John (Z R) L | last post by:
Hi all, I am very new to programming, and I chose to study the Python language before C++. I am currently using the Wikibooks "Non-Programmer's...
7
by: Protected | last post by:
Hello. I'm a complete newbie trying to learn Python. I decided to try some Tkinter examples, including the one from the library reference, but they...
3
by: J-Burns | last post by:
Hello. Im a bit new to using Tkinter and im not a real pro in programming itself... :P. Need some help here. Problem 1: How do I make...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.