为什么在我放入library.zip pygame后找不到freesansbold.ttf

2024-10-02 22:34:44 发布

您现在位置:Python中文网/ 问答频道 /正文

C:\Users\ABDUL HAIYAN\Desktop\Programming\Python\Tic Tac Toe\dist>tictactoe
    pygame 2.0.1 (SDL 2.0.14, Python 3.8.8)
    Hello from the pygame community. https://www.pygame.org/contribute.html
    Traceback (most recent call last):
    File "tictactoe.py", line 181, in <module>
    File "tictactoe.py", line 47, in game_opening
    File "tictactoe.py", line 60, in draw_status
    File "pygame\pkgdata.pyc", line 84, in getResource
    FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\ABDUL HAIYAN
    \\Desktop\\Programming\\Python\\Tic Tac Toe\\dist\\library.zip\\pygame\\freesans
    bold.ttf'

已在其中的文件:

The Files already in there


Tags: inpydistlineticpygameusersfile
1条回答
网友
1楼 · 发布于 2024-10-02 22:34:44

我建议您阅读this page以熟悉pygame.font模块

获取文件的3种方法:

一,。解压缩所需的文件

在这种情况下,应该使用Python打开.zip,然后提取.ttf文件,根据需要打开/读取它,然后删除提取的字体文件

如果希望Python在.zip文件中打开文件,则应使用如下ZipFile模块:

import zipfile # needed to extract the font file
import pygame

archive = zipfile.ZipFile('files.zip', 'r') # the .zip containing your files
font_file = archive.open('font.ttf') # the font.ttf file is ready to be read
archive.close()

pygame.init()
font = pygame.font.Font(font_file, 20) # the font is initialised

... # do some stuff


二,。使用^{}

此模块从系统字体导入字体(即Word中可用的字体等)

语法:

pygame.font.SysFont('font name', font_size)

例如:

font = pygame.font.SysFont('Comic Sans Ms', 20)

在这种情况下,您甚至不需要.ttf文件


三,。运行程序时解压缩文件夹

当然!你为什么要压缩文件夹?解压需要几秒钟的时间…
或者,您可以简单地将文件放在普通文件夹中,而不是.zip

相关问题 更多 >