如何解决pygame_gui与FreeType2未初始化的RuntimeError错误?
pygame_gui初始化报错:FreeType 2库未初始化
问题代码
import pygame import pygame_gui class GUI: def __init__(self, window): self.manager = pygame_gui.UIManager((window.w, window.h)) def init(self): self.hello_button = pygame_gui.elements.UIButton(relative_rect=pygame.Rect((0, 0), (100, 50)), text='UWU', manager=self.manager) def process_events(self, event): if event.type == pygame_gui.UI_BUTTON_PRESSED: if event.ui_element == self.hello_button: print("HI") self.manager.process_events(event) def update(self, dt): self.manager.update(dt) def render(self, window): self.manager.draw_ui(window)
报错信息
Traceback (most recent call last): File "C:\Users\jacki\OneDrive\Desktop\Coding\Python Projects\Uwu123\main.py", line 5, in <module> engine = Engine("C:\Users\jacki\OneDrive\Desktop\Coding\Python Projects\Uwu123\assets") File "C:\Users\jacki\OneDrive\Desktop\Coding\Python Projects\Uwu123\uwu123.py", line 18, in __init__ self.gui = GUI(self) File "C:\Users\USER\OneDrive\Desktop\Coding\Python Projects\Uwu123\gui.py", line 6, in __init__ self.manager = pygame_gui.UIManager((engine.w, engine.h)) File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\pygame_gui\ui_manager.py", line 69, in __init__ self.ui_theme = UIAppearanceTheme(self.resource_loader, self._locale) File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\pygame_gui\core\ui_appearance_theme.py", line 62, in __init__ self.font_dict = UIFontDictionary(self._resource_loader, locale) File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\pygame_gui\core\ui_font_dictionary.py", line 128, in __init__ self._load_default_font() File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\pygame_gui\core\ui_font_dictionary.py", line 156, in _load_default_font error = default_font_res.load() File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\pygame_gui\core\utility.py", line 395, in load self.loaded_font = GUIFontFreetype( File "C:\Users\USER\AppData\Local\Programs\Python\Python310\lib\site-packages\pygame_gui\core\gui_font_freetype.py", line 17, in __init__ self.__internal_font: Font = Font(file, size, resolution=72) RuntimeError: The FreeType 2 library hasn't been initialized
解决方案
调整pygame初始化顺序:pygame_gui依赖pygame的字体系统,必须在创建
UIManager之前完成pygame及字体模块的初始化。在启动代码中添加:import pygame # 初始化pygame核心模块 pygame.init() # 显式初始化字体模块,确保FreeType库被加载 pygame.font.init()确保这段代码在实例化
GUI类之前执行。修改GUI类初始化逻辑:将
UIManager的创建移到init方法中,避免在pygame未完成初始化时提前创建:class GUI: def __init__(self, window): self.window = window self.manager = None self.hello_button = None def init(self): # 此时pygame已完成初始化,再创建UIManager self.manager = pygame_gui.UIManager((self.window.w, self.window.h)) self.hello_button = pygame_gui.elements.UIButton(relative_rect=pygame.Rect((0, 0), (100, 50)), text='UWU', manager=self.manager)随后在Engine类中,先完成pygame初始化,再调用
self.gui.init()。重新安装完整依赖的pygame:如果pygame本身缺少FreeType支持,卸载后重新安装官方wheel包:
pip uninstall -y pygame pip install pygame --upgrade官方wheel包已内置FreeType依赖,无需手动安装系统级库。
内容的提问来源于stack exchange,提问作者bessieTheBoy
相关产品推荐
相关产品推荐

