0

I have that issue with python interpreter - it's closing immediately after script execution. I'm trying to learn pygtk; I wrote "hello world" after tutorial and all I can see is a quick flash of two windows, one interpreter's and one gtk's. I tried to run the script from command.com instead of by double-click - didn't help much.

In older Windows I'd simply check the checkbox on apropriate tab, but how do I do it on this frelling eye candy?

5 Answers5

2

I am not familiar with pygtk, but if you are double-clicking on a Python program under Windows, and the program runs and then the window closes, then you can try this simple approach by putting a call to

 raw_input() # with Python 2.x

or

 input() # with Python 3.x

at the end of your script. This will cause the program to stop and wait for an input (and keep your window open), and if you hit the key the program will end (and your window will close).

Levon
  • 138,105
  • 33
  • 200
  • 191
2

It is often the case that some exception happens and even if you have placed some sleep function (or some other statement waiting for the user) at the end, it may fail.

To overcome that, just use exception catching and its part after finally:

import time

try:
    ... (your code goes here) ...
except:
    ... (some exception handling, if needed) ...
finally:
    time.sleep(5)  # always sleep 5 seconds before exiting

The part after finally ensures that some code will be executed in both cases (with exception and without exception). Unless you manually switch off your computer ;)

Instead of time.sleep you can also use input(), as suggested by Mark Lutz in his "Learning Python. 4th Edition" book.

Tadeck
  • 132,510
  • 28
  • 152
  • 198
  • (not the downvoter) I think you need a way to actually print the exception before closing (with your method: if you put something in except, it will suppress the output, if you don't put except the exception will be printed, but only after the sleep! – Antonio Mar 12 '14 at 13:20
1

If you're running a script from a command window the python -i option will keep the interactive Python session open after the script finishes so you can enter Python statements for debugging, etc.:

C:\>python -i script.py
>>>

Not sure if this will keep the GTK window open though, depends on how you programmed it I expect.

Don O'Donnell
  • 4,538
  • 3
  • 26
  • 27
0

I suggest that you run your code in some sort of editor (pretty much any will do: Eclipse, Geany, Spyder, IDLE, etc.). The reason for this is that when the program is executed, you are most likely getting a fatal error in your code somewhere. Python IDEs have a tendency to keep the windows used to execute the code open even if there is an error, so you can see the error callback for reference. That will tell you where you are getting the error, and will allow you to fix the code accordingly. of course, you could always just tack on input() or time.sleep() to the end and hope that helps, but that's up to you. I recommend using an IDE like one I mentioned above.

Ben Schwabe
  • 1,283
  • 1
  • 21
  • 36
  • 1
    You do not need IDE for that. See [the answer Tadeck provided](http://stackoverflow.com/a/12344347/1070433). – zizozu Sep 10 '12 at 20:59
0
import subprocess
print "hello from python"

subprocess.Popen(["pause"], shell=True)

this will wait for any key pressed

Alex Okrushko
  • 7,212
  • 6
  • 44
  • 63