首页 > 解决方案 > 如何在我的 python 项目中本地引用文件?

问题描述

我正在尝试创建一个 exe“游戏”(更多的是概念验证),但是当我这样做时,图像文件不显示。:

parentDirectory = os.path.abspath(os.path.join(os.getcwd(), os.pardir))
photo = tk.PhotoImage(file = os.path.join(parentDirectory, 'okokokokok1.1','imgs', 'spooky.PNG'),width=320,height=240)

老实说,我不明白出了什么问题,请帮助 thx ;3

标签: pythongithub

解决方案


@furas 解决了我的问题,谢谢。他们的评论:

而不是os.getcwd()你应该使用os.path.dirname(os.path.abspath(__file__)),因为getcwd给了Current Working Directory这不一定是Directory with Script


推荐阅读