Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing Pygame Window

Tags:

python

pygame

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:

import pygame
import time
(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
pygame.display.flip()
pygame.display.set_caption("Hello World")
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

When I append

time.sleep(5)
pygame.quit()

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.

like image 745
KnightOfNi Avatar asked Nov 09 '13 20:11

KnightOfNi


People also ask

How do I close a pygame window?

Call pygame. quit() to shut down pygame (this includes closing the window) Call sys. exit() to shut down the program (this exits the infinite loop)

How do I get out of fullscreen on pygame?

Just pressing the ⊞ (window) key.

What triggers pygame quit?

pygame. QUIT is sent when the user clicks the window's "X" button, or when the system 'asks' for the process to quit.


6 Answers

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.

like image 74
Erikun Avatar answered Sep 26 '22 17:09

Erikun


if you want to make pygame close when window button x is pressed, put the code like this:

from sys import exit
while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()

We put exit() after pygame.quit(), because pygame.quit() makes the system exit and exit() closes that window.

like image 40
jerryalfs Avatar answered Sep 25 '22 17:09

jerryalfs


Not sure but try this Because you code runs fine on my system after I add pygame.quit() at the end

import pygame
import time
(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
pygame.display.flip()
pygame.display.set_caption("Hello World")
running = True
try:
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
    pygame.quit()
except SystemExit:
    pygame.quit()

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

like image 36
xor Avatar answered Sep 26 '22 17:09

xor


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

like image 22
blissweb Avatar answered Sep 26 '22 17:09

blissweb


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!

like image 40
Jose Pablo Jimenez Avatar answered Sep 22 '22 17:09

Jose Pablo Jimenez


Add this at the top:

import sys

Add this where you need to quit:

if event.type == pygame.QUIT:
    pygame.quit()
    sys.exit()
like image 45
FortCraftCoder Avatar answered Sep 25 '22 17:09

FortCraftCoder