Python Forum
Unable to exit Pygame window - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: Unable to exit Pygame window (/thread-25341.html)



Unable to exit Pygame window - Hesper - Mar-27-2020

Hello friends I am new to pygame and python. The thing is I am not able to exit the python program even after I click the exit button on the pygame window. Here is my code:
import pygame
import sys

pygame.init()

WIDTH = 800
HEIGHT = 600

screen = pygame.display.set_mode((WIDTH, HEIGHT))

gameOver = False

while not gameOver:
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			sys.exit()
And instead I get this as output:
Output:
Traceback (most recent call last): File "/home/my-dell/dev/Documents/Programming projects/Practical_Python/atbsp.py", line 16, in <module> sys.exit() SystemExit
Thanks in advance. I am using Ubuntu 16.04 LTS.


RE: Unable to exit Pygame window - SheeppOSU - Mar-27-2020

sys.exit will not close the pygame window. You have to use pygame.quit but I'd also suggest making the code more like this.
while not gameOver:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameOver=True
pygame.quit()
sys.exit()
This way it will run through everything and then close out everything as well. Since your new to pygame I'd suggest looking at the tutorial here on this forum. Just go to the tutorial section from the main page --> Game Tutorials --> and there's an entire series for learning pygame by metulburr


RE: Unable to exit Pygame window - Hesper - Mar-30-2020

Thanks Bro I owe you Wink .