Closing Pygame Window

前端 未结 8 705
不知归路
不知归路 2020-12-31 03:24

I just spent a fair amount of time finding a 64-bit installation of pygame to use with python 3.3, (here) and now am trying to make a window. However, although the window op

相关标签:
8条回答
  • 2020-12-31 03:45

    This was the final code that worked for me on OSX whilst keeping the kernel alive on Jupyter. EDIT - it does still crash the kernel sometimes :-(

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
    pygame.display.quit()
    pygame.quit()
    exit()
    

    Also needed to downgrade ipython to get rid of some magic alias warning messages using:

    conda install ipython=7.2.0
    

    apparently that issue is due to be fixed in ipython 7.6.0

    0 讨论(0)
  • 2020-12-31 03:47

    Suffered the same issues on Python 3.7.4 while running it from in IDE (Spyder 3.3.6). In my case the pygame.quit() would not completely close the program. Nonetheless, adding quit() or exit() did the trick for me!

    0 讨论(0)
  • 2020-12-31 03:48

    try using the following command:

    sys.exit(0)

    notice: You will need to import the sys library in order to use it.

    0 讨论(0)
  • 2020-12-31 03:53

    Most pygame tutorials seem to suggest exiting by calling pygame.quit() and then sys.exit(). I have personally run into problems (was on a unix system though) where this still did not close the window properly. The solution was to add pygame.display.quit() specifically before pygame.quit(). That should not be necessary as far as I can tell, and I'm afraid I don't know why that solved the issue but it did.

    0 讨论(0)
  • 2020-12-31 03:54

    To answer the original question: You must call pygame.quit() after breaking the main loop. One elegant solution goes as follows:

    def run():
        pygame.init()
        while True:
            # ...
            for event in pygame.event.get():
                # Handle other events
                if event.type == pygame.QUIT:
                    return pygame.quit()
    
    0 讨论(0)
  • 2020-12-31 03:55

    The IDE interferes with how pygame runs the code. Try to run it from the commandline or the terminal. The problem should disappear.

    0 讨论(0)
提交回复
热议问题