BJYackley
BJYackley

Reputation: 21

pygame.quit() hangs, even outside IDLE

I'm using Pygame 1.9.1 with Python 2.7.3 on Debian,

I am trying to get a small test program working before I get any more advanced;
Things seem to work fine up until the point where I tell Pygame to quit.

I've read that you need pygame.quit(), at least inside IDLE (to make sure it quits cleanly,)
So I've got that - but it still froze.

The relevant code looks something like this:

def mainLoop():                                                                                                                 
    running = True                                                                                                              
    clock = pygame.time.Clock()                                                                                                 
    while running:                                                                                                              
        gameMode()                                                                                                              
        render()                                                                                                                
        key = pygame.key.get_pressed()                                                                                          
        for event in pygame.event.get():                                                                                        
            if event.type == QUIT or key[K_ESCAPE]:                                                                             
                running = False                                                                                                 
                print 'finished'                                                                                                
                return                                                                                                          
        clock.tick(30)

if __name__=='__main__':                                                                                                        
    mainLoop()                                                                                                                  
    print 'exiting after main loop'                                                                                             
    pygame.quit()

I run this and it prints out both 'finished' and 'exiting after main loop',
but even then hangs with the window still present.

Even more strangely,
I've pared it down to a minimal program that still hangs (that's this, in its entirety):

import pygame
pygame.init()
pygame.quit()

Could someone shed some light on what's going on here?

Upvotes: 2

Views: 1937

Answers (4)

Tuckertcs
Tuckertcs

Reputation: 29

even when using IDLE, I did both:

# other code ^
pygame.quit()
sys.exit()

and that seemed to work

Upvotes: 0

Harry Cutts
Harry Cutts

Reputation: 1414

pygame.init will try to initialise all of PyGame's modules, whether needed or not. pygame.quit will quit all active modules, and is called automatically when the interpreter quits (which is why the issue still remains when the explicit call to pygame.quit is removed).

The pygame.mixer.quit method currently hangs on Debian Wheezy (as of 2013-06-12). If you are not using the mixer, you can just replace calls to pygame.init with calls to the init methods of the modules which you are using (a list of modules which require init calls can be found under 'I' in the PyGame documentation index). In my case, initialising the display module was sufficient. So, your example becomes:

import pygame
pygame.display.init()
pygame.quit()

Upvotes: 5

Steffen Søborg
Steffen Søborg

Reputation: 1

are you trying to run the program in IDLE? ive heard you are not suppose to do that since IDLE does not like pygame

Upvotes: 0

ThisIsAQuestion
ThisIsAQuestion

Reputation: 1967

The command pygame.quit() basically just closes the window that you have open. The code will then continue until it reaches an error, which will be the screen not existing. If you want to actually stop the program, use sys.exit() and the program wil stop.

Upvotes: 0

Related Questions