首页 > 解决方案 > autopytoexe 无法识别文件夹图像?

问题描述

我正在尝试使用单一文件方法将我的 game.py 传递给具有 auto-py-to-exe 的可执行文件,它完全适用于单一目录方法,但我在加载一些我认为的图像时遇到问题。

问题是,我激活了所有调试,首先我遇到了要加载的根文件的问题,但我设法让它工作,你只需要将 resource_path 与图像混合(在这种情况下):

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
    return os.path.join(base_path, relative_path)


# example of what I had to do so it got me the images right
background_img = pygame.image.load(resource_path("fondo_memory.png"))

好的,所以根文件清除了,一切都很好,但是使用文件夹图像,我将它们添加到 autopytoexe 界面的附加文件中,但我认为程序没有正确处理它们在此处输入图像描述

这就是我尝试从 Imgs 文件夹加载图片的方式,并像根文件一样添加 resource_path:

index  = 0
for filename in os.listdir():
    if filename.startswith("Imgs"):
        images = []
        cont = 0
        files = os.listdir(filename)
        random.shuffle(files)
        for file in files:
            img = pygame.image.load(resource_path(os.path.join(filename, file)))
            print(resource_path(os.path.join(filename, file)))
            img.set_colorkey(WHITE)
            
            if cont < 6:
                images.append(GameImage(img))
            cont += 1

        game_images_dict[index] = images
        index += 1

我确实将其更改为添加资源路径,但它仍然不起作用,打印 (resource_path(os.path.join(filename, file))) 给了我这个:

在此处输入图像描述

所以是的,不知道为什么它不起作用,因为我可以使用我在根目录中初始化的元素启动游戏,但是当我进入文件夹图像应该存在的场景时,它不显示任何事物。我认为这与加载图像有关,但我可能错了。

标签: pythonpygamepyinstaller

解决方案


推荐阅读