pyinstaller 打包后无法显示图片问题解决

pyinstaller 打包后无法显示图片

解决:

路径转换

import sys
import os

def get_mei_pass(file_path):
    real_path = None
    if hasattr(sys, '_MEIPASS') and os.path.isfile(os.path.join(getattr(sys, '_MEIPASS'), file_path)): 
        real_path = os.path.join(getattr(sys, '_MEIPASS'), file_path) 
    elif os.path.isfile(os.path.join(os.path.dirname(os.path.realpath(__file__)), file_path)): 
        real_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), file_path)
    else:
        print("mei_pass error")
    print("mei_pass: " + real_path)
    return real_path

 调用:

        ico_path = get_mei_pass("res/icon.ico")
        icon = QIcon(ico_path)
posted @ 2024-12-26 17:21  hxqmw  阅读(307)  评论(0)    收藏  举报