python - 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))) 给了我这个:
所以是的,不知道为什么它不起作用,因为我可以使用我在根目录中初始化的元素启动游戏,但是当我进入文件夹图像应该存在的场景时,它不显示任何事物。我认为这与加载图像有关,但我可能错了。
解决方案
推荐阅读
- ruby-on-rails - 父对象只拾取 has_many 中的第一个子对象
- php - 如何使用 Jquery Ajax 将表单输入字段发送为 JSON 格式
- java - 确定回文的程序只执行 else 条件
- python - 获取具有相同数字和连续索引的子数组
- json - Alexa 一直回复“请求的技能的响应有问题”
- php - 如何使用 json_encode 对两个数组进行编码?
- sql-server - SQL Server CONTAINS 无法按预期工作
- kotlin - 如何在 kotlin 中安排重复任务?
- jquery - $('tbody tr')[0].offset().top 不起作用
- python - 窗口化后无法批处理数据集?