Python 官方文档:入门教程 => 点击学习
python3 编译器 PyCharm今天在处理pygame的字体时遇到了一些问题,程序如下:import pygame.ftfont class Button(): def __init__(self, ai_seting
今天在处理pygame的字体时遇到了一些问题,程序如下:
import pygame.ftfont
class Button():
def __init__(self, ai_setings, screen, msg):
'''初始化按钮的属性'''
self.screen = screen
self.screen_rect = screen.get_rect()
# 设置按钮的尺寸和其他属性
self.width, self.height = 200, 50
self.button_color = (0, 255, 0)
self.text_color = (255, 255, 255)
self.font = pygame.font.Font(r'fonts\freesansbold.ttf', 48)
# 创建按钮的rect对象,并使其居中
self.rect = pygame.Rect(0, 0, self.width, self.height)
self.rect.center = self.screen_rect.center
# 按钮的标签只要创建一次
self.prep_msg(msg)
上面的程序时可以正常运行的,其中被高亮的代码是会出错的地方,课本上的源代码是self.font=pygame.font.Sysfont(None,48),但是编译后报错,错误原因如下:
OSError: unable to read font file
在网上查找原因可能有两个,一个是字体文件.ttf损坏,但我重下了该字体文件替换了却还是会报错,另一个原因是路径出错,解决办法是将self.font=pygame.font.Sysfont(None,48)替换为self.font=pygame.font.Font(None,48),并将第一个参数改为字体文件的具体目录。我的代码中,是在我的这个项目的文件夹中新建了一个fonts文件夹,并将.ttf文件放入。
Sysfont,只需要字体文件的名字,Font需要字体文件的全目录。
--结束END--
本文标题: python font的处理
本文链接: https://www.lsjlt.com/news/190659.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0