Pygame programs hanging on exit

前端 未结 5 1457
野趣味
野趣味 2021-01-02 07:11

I\'m tinkering around with pygame right now, and it seems like all the little programs that I make with it hang when I try to close them.

Take the following code, fo

相关标签:
5条回答
  • 2021-01-02 07:26

    If you are running it from IDLE, then you are missing pygame.quit().

    This is caused by the IDLE python interpreter, which seems to keep the references around somehow. Make sure, you invoke pygame.quit() on exiting your application or game.

    See: In IDLE why does the Pygame window not close correctly?

    And also: Pygame Documentation - pygame.quit()

    0 讨论(0)
  • 2021-01-02 07:30

    I had the same problem, but solved it by doing the following:

    try:
       while True:
          for event in pygame.event.get():
             if event.type==QUIT or pygame.key.get_pressed()[K_ESCAPE]:
                pygame.quit()
                break
    finally:
       pygame.quit()
    
    0 讨论(0)
  • 2021-01-02 07:37

    I had a similar problem on knowing why I can't close pygame windows.. and searched.. and came across this..

    I think this explains everything.. and good idea too..

    as seen in: http://bytes.com/topic/python/answers/802028-pygame-window-not-closing-tut-not-helping

    I think the issue is that you're running it from within IDLE. It looks like pyGame's event loop and Tkinter's event loop interfere with each other. If you run the scripts from the command line, it works.

    0 讨论(0)
  • 2021-01-02 07:46

    'if event.type==QUIT' generates a syntax error. Should be == pygame.QUIT Also, the rest of the line is incorrect but I can't see how. There's a cleaner variant here:

        running = True
        while running:
           for event in pygame.event.get():
               if event.type == pygame.QUIT:
               running = False
        pygame.quit()
    
    0 讨论(0)
  • 2021-01-02 07:47

    Where do you exit the outer loop?

     while True: # outer loop
         for event in pygame.event.get(): # inner loop
             if event.type == QUIT:
                break # <- break inner loop
    
    0 讨论(0)
提交回复
热议问题