The line
event.type == pygame.QUIT
does nothing. Its returning a boolean (true/false) which you dont capture in a variable or use in any conditional statement (e.g. if).
What you should do is set run
to False when you detect the key and your loop will exit before the next iteration.
Here is an example integrated with an application that listens for events, which includes key presses.
import pygame
pygame.init()
pygame.display.set_caption("key quitter")
screen = pygame.display.set_mode((240,180))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_BACKSPACE:
running = False
print('ending successfully')
pygame.quit()
Note the the normal quit from the gui (top right [x] on windows) also works.