原本使用直接调用方式,打包时报错
FileNotFoundError: [Errno 2] No such file or directory:
为方便打包,将待使用文件nicubunu_Lock.png放至res文件夹下。
如果希望将图片共同打包,需将原直接调用图片的路径改为相对路径。增加get_resource_path(relative_path) ,并将原程序中图片调用路径更改。
# ------------------ res path setting ----------------------- #
"""
to intergrate the image used as background in to executable program
"""
def get_resource_path(relative_path):
if getattr(sys, 'frozen', False):
base_path = sys._MEIPASS
else:
base_path = os.path.abspath('.')
return os.path.join(base_path, relative_path)
图片调用程序更改为:
# Load an image in the script
img_path = get_resource_path(os.path.join('res', "nicubunu_Lock.png"))
img = Image.open