我想用pyinstaller構建一個文件exe,但我不確定圖像的文件路徑應該在主python文件中。pyinstaller捆綁圖像的文件路徑?
在我使用的MEIPASS代碼我主要的Python文件的頂部:
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
這是當前的代碼,我爲每個圖像文件:
root.iconbitmap('C:\\Users\\username\\Downloads\\TripApp\\BennySM.ico')
filename = PhotoImage(file = 'C:\\Users\\username\\Downloads\\TripApp\\BgSM.gif')
我知道這些不是最好的文件路徑,但我不知道我需要添加什麼,所以python文件知道在哪裏看。圖像與exe文件捆綁在一起,如果我將exe文件添加到數據文件中,它會找到圖像並運行。
謝謝!我之前嘗試添加resource_path,但我在頂部的define部分中缺少文件路徑。
再次感謝!