简体   繁体   中英

PyGame not quitting after pygame.quit() and sys.exit(0) are called

I was doing some testing with the Pygame module, however when I quit the program using the following piece of code, the Pygame launcher application ( Image here ) does not quit after the I close the window (It required force quit). Is there a way to fix this?

import sys, pygame
pygame.init()

# Code for creating window surface, putting things on it, etc.

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit(0)

I tried to look it up, but none of the offered solutions work. Any help is appreciated.

I'm not sure if it's gonna work but you better try it. I add some variable for while loop

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

pygame.quit()
sys.exit()

Figured out an alternative. Although this uses the OS module (and a protected method which I personally recommend against), it does the job pretty well.

while True:
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        os._exit(0) # Forcibly exit the program

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