Pygame项目加载PNG失败:libpng16-16.dll无法被系统访问
Pygame加载PNG图片失败:无法访问libpng16-16.dll
使用Pygame和pygame_gui开发项目时,所有PNG图片都无法加载,报错提示无法加载libpng16-16.dll,系统无法访问该文件。已尝试重新安装libpng,也将该DLL复制到site-packages目录下的多个文件夹,但问题仍未解决。
报错堆栈
Traceback (most recent call last): File "C:\Users\tomto\PycharmProjects\CasinoPy\main.py", line 82, in <module> test = pygame.image.load('Assets/Cards/2_of_Hearts.png') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pygame.error: Failed loading libpng16-16.dll: The file cannot be accessed by the system. Process finished with exit code 1
项目代码
import pygame import pygame_gui from pygame_gui.core import ObjectID import random import sys pygame.init() RED = (255, 0, 0) WHITE = (255, 255, 255) BLACK = (0, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) SCREEN_SIZE = (1000, 600) SUITS = ['Hearts', 'Diamonds', 'Clubs', 'Spades'] VALUES = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'jack', 'queen', 'king', 'ace'] deck = [] screen = pygame.display.set_mode(SCREEN_SIZE) pygame.display.set_caption("Casino") clock = pygame.time.Clock() running = True manager = pygame_gui.UIManager(SCREEN_SIZE, 'button.json') manager.get_theme().load_theme('label.json') class Menu: def __init__(self): self.running = True self.title = pygame_gui.elements.UILabel(relative_rect=pygame.Rect((400, 100), (200, 50)), text='Casino', manager=manager, object_id=ObjectID(class_id='@menuText', object_id='#title')) self.blackjackButton = pygame_gui.elements.UIButton(relative_rect=pygame.Rect((400, 200), (200, 50)), text='Blackjack', manager=manager, object_id=ObjectID(class_id='@menuButtons', object_id='#blackjackButton')) self.pokerButton = pygame_gui.elements.UIButton(relative_rect=pygame.Rect((400, 300), (200, 50)), text='Poker', manager=manager, object_id=ObjectID(class_id='@menuButtons', object_id='#pokerButton')) self.loginButton = pygame_gui.elements.UIButton(relative_rect=pygame.Rect((400, 400), (200, 50)), text='Login', manager=manager, object_id=ObjectID(class_id='@menuButtons', object_id='#loginButton')) def draw(self): while self.running: time_delta = clock.tick(60) / 1000.0 screen.fill(WHITE) for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False if event.type == pygame_gui.UI_BUTTON_PRESSED: if event.ui_element == self.blackjackButton: print("Start Game") elif event.ui_element == self.pokerButton: pass manager.process_events(event) manager.update(time_delta) manager.draw_ui(screen) pygame.display.update() clock.tick(60) class Card: def __init__(self, suit, value): self.suit = suit self.value = value self.name = f'{value}_of_{suit}' self.imagePath = f'Assets/Cards/{self.name}.png' self.image = pygame.image.load(self.imagePath) test = Card('Hearts', '2') menu = Menu() menu.draw()
解决方案
- 检查DLL文件权限:右键
libpng16-16.dll,进入「属性」→「安全」面板,确认当前用户拥有「读取和执行」权限,缺少则点击「编辑」添加对应权限。 - 将DLL放入Python核心目录:把
libpng16-16.dll复制到Python安装根目录(如C:\Python311)或Python311\Scripts目录,这两个路径属于系统默认搜索路径,更容易被Pygame识别。 - 重装Pygame官方预编译包:卸载现有Pygame后,用pip安装官方wheel包,该包已内置所有依赖DLL,无需手动配置:
pip uninstall pygame -y pip install pygame - 排查杀毒软件拦截:部分杀毒软件会误判DLL为恶意文件并拦截,暂时关闭杀毒软件或把
libpng16-16.dll加入白名单后重试。 - 验证图片路径正确性:改用绝对路径加载图片,避免相对路径出错,示例代码:
import os # 在Card类的__init__方法中修改 self.imagePath = os.path.join(os.path.dirname(__file__), 'Assets/Cards', f'{self.name}.png') self.image = pygame.image.load(self.imagePath)
内容的提问来源于stack exchange,提问作者Tom Tooley
相关产品推荐
相关产品推荐

