pygame.error: font not initialized的解决及init()到底干了什么

环境
Python3.6.8
pygame1.9.4html

贴上报错源码:

import pygame
my_font = pygame.font.SysFont('arial', 16)
my_font = pygame.font.Font('my_font.ttf', 16)

报错内容:

Traceback (most recent call last):
  File "C:\Users\HaoziHuang\Desktop\pygame\4\4.py", line 6, in <module>
    my_font = pygame.font.SysFont('arial', 16)
  File "D:\rjyj\Python\python3.6.8\lib\site-packages\pygame\sysfont.py", line 320, in SysFont
    return constructor(fontname, size, set_bold, set_italic)
  File "D:\rjyj\Python\python3.6.8\lib\site-packages\pygame\sysfont.py", line 243, in font_constructor
    font = pygame.font.Font(fontpath, size)
pygame.error: font not initialized

无论先执行哪个字体语句都会报错,
当发生此错误时
这时咱们该检查
程序开始部分是否缺乏pygame的初始化语句pygame.init()
而咱们想问了, pygame.init()到底初始化个啥呀???
这个问题问得好!python

如下内容转载自:简明现代魔法, 感谢大佬的分享

你究竟有(init)几个好(子)妹(模)妹(块)?

当咱们在init()的时候,咱们在干什么

init 这个单词在咱们用python进行面向对象开发的时候是跑不了的。理解python的__init__()其实就是和这里的init()做用差很少。作的工做都是__初始化__.至于他在干什么,个人解释是这样的:shell

咱们已经知道python有一个特殊的“工具包(模块)”叫 pygame了。在咱们要动手用它完成咱们的想法以前,电脑这个强迫症须要咱们检查一遍,这个工具包是否完整,可否正常给咱们提供帮助。而这个检查的动做,就是 pygame.init()

那么init()实际上检查了哪些东西呢?

这个其实也不难实验。直接在shell里面,我执行了这个函数:安全

>>> import pygame
>>> pygame.init()
(6, 0)

不明因此的,他给了我一个元组(6,0),我也很不理解,这个6和0分别表明什么意思。因此查阅了pygame的官方文档函数

initialize all imported pygame modules

init() -> (numpass, numfail)工具

Initialize all imported pygame modules. No exceptions will be raised if a module fails, but the total number if successful and failed inits will be returned as a tuple. You can always initialize individual modules manually, but pygame.init() is a convenient way to get everything started. The init() functions for individual modules will raise exceptions when they fail.测试

You may want to initialize the different modules separately to speed up your program or to not use things your game does not.字体

It is safe to call this init() more than once: repeated calls will have no effect. This is true even if you have pygame.quit() all the modules.ui

初始化全部导入的pygame模块。若是模块失败,则不会引起异常,但若是成功且失败的总数将做为元组返回。您能够 随时手动初始化单个模块,但pygame.init()初始化全部导入的pygame模块是一种方便的方法来启动全部内容。各个模块的init()函数会在失败时引起异常。

您可能但愿单独初始化不一样的模块以加速您的程序或不使用您的游戏没有的东西。this

不止一次调用此init()是安全的:重复调用将不起做用。即便你有pygame.quit()全部模块也是如此。

关于init()的一个意外的实验

我之前历来没有深究过pygame.init()这个函数究竟init了哪些模块,仅仅在实践的过程当中知道,音频播放和建立文字字体的时候,若是没有init就会报错。

今天我在安装个人新的电脑环境的时候,由于不知道电脑的型号,因此并无特地去搜索和安装电脑对应的驱动。结果在安装完python以后,安装pygame(wheel也要安装)以后,运行常规的测试函数pygame.init()返回的数字是(5,1)

排除问题的方法就是把已知能够init()的子模块都先运行掉。通过排查,发现pygame没法调用声卡驱动。剩下的事情就好办不少了,从新安装一下声卡驱动,重启以后就能够正常init了。

可是在这个过程当中,我能够得出比之前更加接近实际的一个结论:

pygame.init()在作的,其实就是检查,电脑上一些须要的硬件调用接口、基础功能是否有问题。若是有,他会在程序运行以前就反馈给你,方便你进行排查和规避。

说了这么多,它到底init了哪些子模块

>>> import pygame
>>> pygame.init()
(6, 0)

>>> pygame.display.init()
>>> pygame.font.init()
>>> pygame.joystick.init()
>>> pygame.mixer.init()

>>> pygame.scrap.init()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
pygame.error: No display mode is set

>>> pygame.freetype.init()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'pygame' has no attribute 'freetype'
>>> pygame.midi.init()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'pygame' has no attribute 'midi'
>>> pygame.cdrom.init()

我把pygame官网上面的doc里介绍的全部带有init的子模块都运行了一遍。

其中midifreetype这两个模块已经没有了(吐槽一下官方的文档吧,都没了还放着嘛)。

scrap初始化失败是由于没有窗口。这样的话,其实已经有5个模块是被初始化了。可是scrap在没有窗口的状况下会报错,到底算不算一个init。还须要后面再仔细看看文档和源码吧。

That's all!再次感谢这位大佬的分享!

附上官方文档

相关文章
相关标签/搜索