简体   繁体   中英

When I close PyGame window the window closes but python becomes non responsive

I wrote a game with pygame. The game works fine but when I quit and close the window, while the window does close Python itself does not quit and instead shows (not responding) and I am forced to, well, force quit.

I start the code by importing several modules

import random, pygame, sys
from pygame.locals import*

There is only one place in the code that directly commands the program to shut down

def terminate():
    pygame.quit()
    sys.exit()

To quit the program always runs the terminate() function.

I tried adding pygame.display.quit() above pygame.quit() and substituting raise SystemExit for sys.exit()

What could cause python to close the window but not shut down itself?

I personnay don't imoprt sys to exit. It simply isn't effective for me to type sys.exit() and you can simply call exit() to exit the program.

So you can close the window like that:

# main loop
running = True
while running:
    for event in pygame.event.get():
        if event.type == QUIT: # if the user closes the window
            running = False


# after exiting mail loop
pygame.quit()
exit()

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