Python Forum

Full Version: Unable to exit Pygame window
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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
Thanks Bro I owe you Wink .