简体   繁体   中英

Pygame window immediately opens and closes

Originally after I did some searching here, I found a question with the exact same problem I had: Pygame window not responding after a few seconds . I reviewed all the answers and tried them, but none of them worked. I tried using for loops to loop through every single event;

run = True
while run:
for event in pygame.event.get():
   if event == pygame.QUIT()
       run = False

But the window still closed. I also tried:

run = True
while run:
    event = pygame.event.get()
    if event == pygame.QUIT():
        run = False

Which had the same results as the one above. Can anyone help? Edit: I use PyCharm and MacOS Catalina.

pygame.QUIT is a constante, but pygame.QUIT() is a call statement. Remove the braces. Anyway, the condition won't work, because you have to compare the type attribute of the event to the event type constant (see pygame.event ). Furthermore the : is missing at the end of the if -statement.

if event == pygame.QUIT()

if event.type == pygame.QUIT:

Furthermore the Indentation is not correct:

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM