Python Forum
Unable to exit Pygame window
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to exit Pygame window
#1
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.
Reply
#2
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.
1
2
3
4
5
6
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
Reply
#3
Thanks Bro I owe you Wink .
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to install pygame (but it seems to already be installed) garthE 1 1,736 Apr-15-2025, 05:02 PM
Last Post: jassonadder
  Testing resolution for pygame window menator01 3 1,862 Aug-30-2024, 09:49 PM
Last Post: menator01
  [PyGame] Cannot display anything inside pygame window the_gullwing 5 3,612 Dec-07-2023, 02:44 AM
Last Post: Benixon
  [PyGame] drawing images onto pygame window djwilson0495 1 4,669 Feb-22-2021, 05:39 PM
Last Post: nilamo
  pygame get window position vskarica 3 8,507 Oct-18-2020, 05:14 PM
Last Post: nilamo
  [pyGame] More Text in one Window, example needed ! JamieVanCadsand 1 4,418 Oct-03-2017, 03:42 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020