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:
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.
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
  [PyGame] Cannot display anything inside pygame window the_gullwing 5 1,027 Dec-07-2023, 02:44 AM
Last Post: Benixon
  [PyGame] drawing images onto pygame window djwilson0495 1 3,423 Feb-22-2021, 05:39 PM
Last Post: nilamo
  pygame get window position vskarica 3 5,811 Oct-18-2020, 05:14 PM
Last Post: nilamo
  [pyGame] More Text in one Window, example needed ! JamieVanCadsand 1 3,504 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