Arkodeep Ray
Arkodeep Ray

Reputation: 174

I get a pygame error after a quit my pygame window

I am trying to understand how to display fonts in pygame. But i get an error saying pygame.error: Library not initialized

This error happens after i press the cross button or quit my pygame window.

Can anyone tell my why this error is happening and how can i fix it please?

import pygame
from pygame.locals import *
import sys
from win32api import GetSystemMetrics
pygame.init()
WIDTH = GetSystemMetrics(0)
HEIGHT = GetSystemMetrics(1)-64
WIDTH_HEIGHT = (WIDTH, HEIGHT)

WINDOW = pygame.display.set_mode(WIDTH_HEIGHT)
pygame.init()
font = pygame.font.Font('freesansbold.ttf', 32)
text = ""

running = True

while running:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            running = False
    text = font.render('Hi', True, (255,255,255))
    WINDOW.blit(text, (0, 0))
    pygame.display.update()

Upvotes: 1

Views: 160

Answers (1)

Matiiss
Matiiss

Reputation: 6156

This is one way to exit (just finishes the program):

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

This is another way (a bit more forceful):

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

Upvotes: 1

Related Questions