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 op
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 :-(
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.quit()
pygame.quit()
exit()
Also needed to downgrade ipython to get rid of some magic alias warning messages using:
conda install ipython=7.2.0
apparently that issue is due to be fixed in ipython 7.6.0
Suffered the same issues on Python 3.7.4 while running it from in IDE (Spyder 3.3.6). In my case the pygame.quit() would not completely close the program. Nonetheless, adding quit() or exit() did the trick for me!
try using the following command:
sys.exit(0)
notice: You will need to import the sys library in order to use it.
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.
To answer the original question: You must call pygame.quit()
after breaking the main loop. One elegant solution goes as follows:
def run():
pygame.init()
while True:
# ...
for event in pygame.event.get():
# Handle other events
if event.type == pygame.QUIT:
return pygame.quit()
The IDE interferes with how pygame runs the code. Try to run it from the commandline or the terminal. The problem should disappear.