pygame.error: video system not initialized. pygame.init() already called

Question:

This is pretty much the simplest code you can make in pygame. All it does is create a window and allow you to close it. However, I am getting this error
pygame.error: video system not initialized. I searched online for this and it seems most people forget to call pygame.init(). I am not sure why I am getting this error.

import pygame
pygame.init()
screen = pygame.display.set_mode((900,500))
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
Asked By: Jonathan

||

Answers:

Just add sys.exit(0) and the end and you are done

Here is some info from official document

pygame.quit() uninitialize all pygame modules quit() -> None
Uninitialize all pygame modules that have previously been initialized.
When the Python interpreter shuts down, this method is called
regardless, so your program should not need it, except when it wants
to terminate its pygame resources and continue. It is safe to call
this function more than once: repeated calls have no effect.

Note, that pygame.quit()uninitialize all pygame modules will not exit
your program. Consider letting your program end in the same way a
normal python program will end.

here is a sample code for you.

import pygame
import sys
pygame.init()
pygame.display.set_mode((900, 500 ) )


while True :
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit(0)
Answered By: toheedNiaz
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.