Skip to content
Advertisement

Python Pygame after closing with pygame.quit() I can’t open it again without stoping the whole script and restarting it

I want to close the Pygame-Window after a specific event occurred and open it again after another event occurred without stopping the whole script. When I stop pygame with pygame.quit() and want to register a font or do whatever it gives me an error that says that pygame is not initialized so I coded it so it would call pygame.init() again after it closed the pygame window but as I read here pygame thinks it is still initialized and when I call pygame.init() again it won’t really do anything. I don’t get the error anymore but the code gets stuck at that point where I want to use pygame again, after I closed the pygame window with pygame.quit() and initialized it again, and then after 2 seconds, it just stops the script. How can I prevent pygame from doing this and reopen the pygame window without restarting the script?

Thank you.

Advertisement

Answer

What you are looking for is pygame.display.quit()

the following script display a pygame window for 1 second then close it

import pygame 
import time 
pygame.display.set_mode((1024, 668)) 
time.sleep(1) 
pygame.display.quit() 

pygame.quit() is the counterpart of pygame.init(). Not what you’re trying to do.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement