I have a project in Pygame 1.9.2 where I reinitialize the display multiple times, and draw text to the display surface. It works fine until I close the Pygame display and re-initialize it again.
It follows a structure like this:
from pygame import *
init() # this is pygame.init()
running = True
visible = True
textFont = font.SysFont("Comic Sans MS", 12)
for evt in event.get():
if evt.type == QUIT:
visible = False
if evt.type == KEYDOWN:
if evt.key == K_ESCAPE:
visible = running = False
textPic = textFont.render("Hello world!", True, (255,255,255))
pygame.error: Text has zero width
The problem is that
pygame.quit() was called before the program was finished. This causes every Pygame module (e.g.
pygame.font) to be uninitialized.
Instead of relying on
pygame.quit to close the Pygame display, use
pygame.display.quit at the end of every
while running loop. Then put
pygame.quit at the very end of the script to unload the rest of the modules after they're done being used.
pygame.init() again before rendering text won't fix this issue either, because it causes the Pygame display to stop responding. (This might be a bug with Pygame 1.9.2)