“文本宽度为零"重新初始化 Pygame 显示后出错? [英] "Text has zero width" error after reinitializing Pygame display?

查看:74
本文介绍了“文本宽度为零"重新初始化 Pygame 显示后出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Pygame 1.9.2 中有一个项目,我多次重新初始化显示,并将文本绘制到显示表面.它工作正常,直到我关闭 Pygame 显示并再次重新初始化它.

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.

它遵循这样的结构:

from pygame import *
init() # this is pygame.init()
running = True
while running:
    display.set_mode((800,600))
    visible = True
    textFont = font.SysFont("Comic Sans MS", 12)
    while visible:
        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))
        display.get_surface().blit(textPic, (0,0))
        display.flip()
    quit()

这个程序一直工作到第一次关闭显示然后重新初始化,之后我在尝试使用 textFont.render 时收到以下错误:

This program works until the display is closed for the first time and then reinitialized, after which I receive the following error when trying to use textFont.render:

pygame.error: Text has zero width

我正在努力找出问题所在...我知道 "Hello world!" 的宽度大于零.我该如何解决这个问题?

I'm tearing my hair out trying to figure out what's wrong... I know that "Hello world!" has a width greater than zero. How do I fix this problem?

推荐答案

问题在于 pygame.quit() 在程序完成之前被调用.这会导致 每个 Pygame 模块(例如 pygame.font)未初始化.

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.

不是依靠 pygame.quit 来关闭 Pygame 显示,而是在每个 while running 的末尾使用 pygame.display.quit环形.然后将 pygame.quit 放在脚本的最后,以在使用完毕后卸载其余模块.

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() 也不会解决此问题,因为它会导致 Pygame 显示停止响应.(这可能是 Pygame 1.9.2 的错误))

(Calling 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)

这篇关于“文本宽度为零"重新初始化 Pygame 显示后出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆