Active7 months ago
Automatically close an output window from a python script. The pynbody.plot.sph package seems as it should be able to turn the output window on and off within the sph.image function call but I have exhausted all the possibilities and am now resorting to this to get the script running. If you want to make pygame close when window button x is pressed, put the code like this: We put exit after pygame.quit, because pygame.quit makes the system exit and exit closes that window. Share improve this answer. Mar 25, 2013 How to enable Python 3 pip on Windows 10 - Duration: 4:19. Artur Spirin 164,601 views.
Mar 31, 2018 2 Answers. Pygame.display.quit Uninitialize the display module quit - None This will shut down the entire display module. This means any active displays will be closed. This will also be handled automatically when the program exits. Pygame window opens then closes immediately As part of my internship this summer I have to use a python/pygame script written by the previous intern. After much trouble, I have finally gotten pygame installed and 'working' (i.e. It at least imports without error) and figured out how to run the script from Terminal, using the correct command. Jan 04, 2009 Re: PyGame Windows Opens and Then Immediately Closes Yes, upload more code. Make sure you have an application loop of some kind; otherwise it certainly will just open the window.
I have just started college and we are going to be using python. We really have done nothing so I have downloaded the program and done some print commands, and that's it.
When I run my .py file (a
print
command) it immediately closes after appearing. I understand why it does this - it's given the output, so it's done what it needs to do - but I also understand that you can stop this from happening.Prevent Cmd From Closing
I looked around this website and none of the solutions given to this question worked, either that or I didn't understand them.
Is there a simple command I can input to my IDLE editor that will put the program on hold or something? I have tried
input('prompt: ')
as suggested by someone, and that made no difference.If there isn't a command for this, is there a way to change the settings on the computer so that programs don't auto close?
Cristian Ciupitu15.7k55 gold badges4444 silver badges6767 bronze badges
keirbtrekeirbtre57355 gold badges1010 silver badges1414 bronze badges
13 Answers
In Python 3, add the following to the end of your code:
This will cause the program to wait for user input, with pressing ENTER causing the program to finish.
You can double click on your
script.py
file in Windows conveniently this way.MikeMike
The only thing that worked for me -i command line argument.
Just put all your python code inside a .py file and then run the following command;
It means that if you set -i variable and run your module then python doesn't exit on SystemExit. Read more at the this link.
Community♦
AnandShanbhagAnandShanbhag4,02511 gold badge1212 silver badges1313 bronze badges
Open your cmd (command prompt) and run Python commmands from there. (on Windows go to run or search and type cmd)It should look like this:
This will execute your code in cmd and it will be left open.However to use python command, Python has to be properly installed so cmd recognizes it as a command. Checkout proper installation and variable registration for your OS if this does not happen
rodlingrodling45022 gold badges1414 silver badges3939 bronze badges
Run the command using the windows command prompt from your main Python library source.Example.
Brandon PooleBrandon Poole
zx48517.7k1313 gold badges3333 silver badges4949 bronze badges
codercoder
Cristian Ciupitu15.7k55 gold badges4444 silver badges6767 bronze badges
photonsphotons
Depending on what I'm using it for, or if I'm doing something that others will use, I typically just
input('Do eighteen backflips to continue')
if it's just for me, if others will be using I just create a batch file and pause it afterI use the above if there is going to be files renamed, moved, copied, etc. and my cmd needs to be in the particular folder for things to fall where I want them, otherwise - just
JMB2KJMB2K
In Python 2.7 adding this to the end of my py file (
if __name__ '__main__':
) works:SamuelSamuel
Well I got similar issue, It is solved by adding Environment Variable.
Add System Variables in Window
Name :
PYTHONPATH
Value :
C:Python27;
Pygame Quit
Your Python path.
Krunal KapadiyaKrunal Kapadiya
I think I am too late to answer this question but anyways here goes nothing.
I have run in to the same problem before and I think there are two alternative solutions you can choose from.
- using sleep(_sometime)
from time import * sleep(10)
- using a prompt message (note that I am using python 2.7)
exit_now = raw_input('Do you like to exit now (Y)es (N)o ? ')'
if exit_now.lower() = 'n'
//more processing here
Alternatively you can use a hybrid of those two methods as well where you can prompt for a message and use sleep(sometime) to delay the window closing as well. choice is yours.
please note the above are just ideas and if you want to use any of those in practice you might have to think about your application logic a bit.
Pygame Title Screen
Pissu PusaPissu Pusa34622 gold badges77 silver badges2020 bronze badges
Late in here, but in case someone comes here from google---
Go the the location of your .py file. Press SHIFT then right click anywhere and choose open command prompt from here. Once it's up, Just add
'python NameOfTheProg.py' to the cmd line
user3454450user3454450
The reason why it is closing is because the program is not running anymore, simply add any sort of loop or input to fix this (or you could just run it through idle.)
John. BJohn. B
I couldn't find anywhere on the internet a true non-script specific, double click and the window doesn't close solution. I guess I'm too lazy to drag and drop or type when I don't need to so after some experimentation I came up with a solution.
The basic idea is to reassociate .py files so they run a separate initial script before running the intended script. The initial script launches a new command prompt window with the /k parameter which keeps the command prompt open after completion and runs your intended script in the new window.
Maybe there are good reasons not to do this, those with more knowledge please comment if so, but I figure if I run into any it is easy to revert back if needed. One possibly undesirable side effect is dragging and dropping or typing and running from a command prompt now opens a second command prompt rather than running in the command prompt you dragged or typed in.
Now, for the implementation, I call the initial python script python_cmd_k.pyw. I'm using Python 3.7. The code required may differ for other versions. Change the path C:Python37python.exe to the location of your python installation. Associate .pyw files to pythonw.exe (not python.exe) through Windows if they aren't already.
This runs every time you double click any .py script and launches a new command prompt to run the script you double clicked. Running through pythonw.exe suppresses the command prompt window when this initial script runs. Otherwise if you run it through python.exe an annoying blink of a command prompt appear as a result of the first window showing briefly each time. The intended script displays because the code in the initial script above runs the intended script with python.exe.
Now associate .py files with python.exe (not pythonw.exe) through Windows if they are not already and edit the registry entry for this association (Disclaimer: Always back up your registry before editing it if you are unsure of what you are doing). I do not know if there are different paths in the registry for file association for different versions of Windows but for me it is at:
Pygame Exit
Change the data to the pythonw.exe path (not python.exe) and add the path to the ptyhon script above and '%1' as arguments ('%1' passes the full path of the doubled clicked file). For example if pythonw.exe and python_cmd_k.pyw are at C:Python37 then:
It is not necessary to put python_cmd_k.pyw in the same directory as pythonw.exe as long as you provide the correct path for both. You can put these in .reg files for easy switching back and forth between using the script and the default behavior. Change the paths as needed in the examples below (location in the registry, your installation of python, the location you put your python_cmd_k.pyw script).
With ptyhon_cmd_k.pyw (change paths as needed):
Default version (change paths as needed):
BrianZ111BrianZ111
Not the answer you're looking for? Browse other questions tagged pythonwindows or ask your own question.
Active2 months ago
I just spent a fair amount of time finding a 64-bit installation of pygame to use with python 3.3, (here) and now am trying to make a window. However, although the window opens up fine it does not close when it hit the x button. In fact, I have to close IDLE to close the window. I am running a 64 bit version of Win 7. Here is my code:
When I append
It still doesn't close. My only guess would be that pygame.quit might go inside one of the loops, but even if that were resolved I would greatly prefer being able to close the window when I want to.
KnightOfNiKnightOfNi36011 gold badge55 silver badges1717 bronze badges
7 Answers
![Pygame Window Closing Automatically Pygame Window Closing Automatically](/uploads/1/2/5/0/125048475/880349681.jpg)
Most pygame tutorials seem to suggest exiting by calling
pygame.quit()
and then sys.exit()
. I have personally run into problems (was on a unix system though) where this still did not close the window properly. The solution was to add pygame.display.quit()
specifically before pygame.quit()
. That should not be necessary as far as I can tell, and I'm afraid I don't know why that solved the issue but it did.ErikunErikun
if you want to make pygame close when window button x is pressed, put the code like this:
We put exit() after pygame.quit(), because pygame.quit() makes the system exit and exit() closes that window.
Henry YikPygame Quit Event
5,96533 gold badges55 silver badges2323 bronze badges
jerryalfsjerryalfs
Not sure but try this Because you code runs fine on my system after I add
pygame.quit()
at the endIts perhaps because as Idle is made on Tkinter and thus Tkinter and Pygame main loop do not have a mutual understanding.
Your code will run very well on command prompt though.
Your code will run very well on command prompt though.
noobmaster69noobmaster69
This was the final code that worked for me on OSX whilst keeping the kernel alive on Jupyter. EDIT - it does still crash the kernel sometimes :-(
Also needed to downgrade ipython to get rid of some magic alias warning messages using:
apparently that issue is due to be fixed in ipython 7.6.0
blisswebblissweb
try using the following command:
sys.exit(0)
notice: You will need to import the sys library in order to use it.
OphirBackOphirBack
The IDE interferes with how pygame runs the code. Try to run it from the commandline or the terminal. The problem should disappear.
NinjasAtWorkNinjasAtWork
To answer the original question: You must call
pygame.quit()
after breaking the main loop. One elegant solution goes as follows:Bruno CavalleriBruno Cavalleri