pygame.error: font

发布时间:2019-10-11 09:02:19编辑:auto阅读(2016)

    环境
    Python3.6.8
    pygame1.9.4

    贴上报错源码:

    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()到底初始化个啥呀???
    这个问题问得好!

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

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

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

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

    我们已经知道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.

    初始化所有导入的pygame模块。如果模块失败,则不会引发异常,但如果成功且失败的总数将作为元组返回。您可以随时手动初始化单个模块,但pygame.init()初始化所有导入的pygame模块是一种方便的方法来启动所有内容。各个模块的init()函数会在失败时引发异常。

    您可能希望单独初始化不同的模块以加速您的程序或不使用您的游戏没有的东西。

    不止一次调用此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!再次感谢这位大佬的分享!

    附上官方文档

关键字