I am trying to create a window and then close the window. Everything runs smoothly until I click the red X. When I click it, the window doesn't close and all that happens is that the colourful red beach ball thing shows up whenever I hover my mouse over the Pygame window and the window just does not respond.

It seems like the pygame.quit() or pygame.display.quit() functions do not work for some reason.

import pygame
Window=pygame.display.set_mode((250, 250))
while run:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:

This is a copy of the window that shows up when I run the code This is a picture of the console showing that there is not any error with the code

  • 2
    \$\begingroup\$ In the future, please post code as text instead of images. That way people can quickly try to reproduce your issue. \$\endgroup\$ – Tyyppi_77 Dec 30 '18 at 11:35
  • \$\begingroup\$ Try to draw something like a coloured background to the window, also, Why are you using pygame.display.quit()? pygame.quit() alone would do the work. \$\endgroup\$ – Juanpa Dec 30 '18 at 19:54

This is the correct code:

from pygame.locals import *

import sys,pygame

screen=pygame.display.set_mode((200, 100))



   for event in pygame.event.get():

        if event.type==QUIT:


  • \$\begingroup\$ Why is that the correct code? What's missing in the OPs implementation? \$\endgroup\$ – Vaillancourt Aug 19 '19 at 20:03

Well instead of declaring run as variable you can just do it like this:

from pygame.locals import *
import sys

enter code here

while 1:
 for event in pygame.event.get()
  if event.type == QUIT:
  • 1
    \$\begingroup\$ This doesn't seem to answer the question, but just points out an unrelated thing. \$\endgroup\$ – user1118321 Mar 30 '19 at 15:42
  • \$\begingroup\$ Welcome to GDSE & thanks for contributing. As mentioned, this doesn't seem to address OP's core problem & as such it seems more like a comment than an answer. There is a small rep limit before you can comment, but attempting to get around that by posting as answer rarely works out well. If you did intend this more as a comment than a stand alone solution to the problem, flag your answer with a brief explanation for the mods. \$\endgroup\$ – Pikalek Apr 1 '19 at 4:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.