473,789 Members | 2,925 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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


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.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()


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
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.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


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*********@po box.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
639
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 which apparently installs tcl as well. Now I'm trying to run Idle and get this little beauty (similar for other Tkinter apps): ===== E:\Programming\Python2.3\Lib\idlelib>idle.py Traceback (most recent call last):
3
7037
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 also have Win32all-157 installed. from Tkinter import * class App:
1
1896
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 turns up a few false positives <wink>. JDH
1
2213
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 2.3, IDLE 1.0.3. When I execute the code below: from Tkinter import * root = Tk() the window appears form IDLE 0.8, but not from IDLE 1.0.3.
4
2634
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 default python installed on the system. I have installed Tcl/Tk Aqua from http://tcltkaqua.sourceforge.net/. When I run the file called gui.py that contains this: from Tkinter import * root = Tk() root.title("Simple GUI")
1
3006
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 I'm testing the "hello world" that I found in the documentation. I first launch IDLE, write (cut and paste from the web) the program in an editing window, save it, and then run it. The window with two buttons is displayed in the back of the IDLE window (and I cannot bring it to the front),...
6
5254
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 Tutorial for Python", and am up to the section "Who goes there"? http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python/Who_Goes_There%3F But after clicking "run module" for " a = 1
7
3283
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 don't seem to do anything! Shouldn't there be, like, a dialog? I'm running Windows XP and using IDLE. You can assume my version of Python is the latest.
3
3918
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 something appear on 2 separate windows using Tkinter? By this I mean that the format would be something like this: You have Page1 : This has 2-3 buttons on it. Clicking on each button opens up a new window respectively having
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9984
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.