Python pyinstaller的捆绑数据错误(--onefile)

Python pyinstaller的捆绑数据错误(--onefile),python,pyinstaller,Python,Pyinstaller,我想为我的.exe添加一个图像。我正在按照上面列出的方向走。它说构建在终端中成功完成。当我运行.exe时,它会在程序中标记一个致命错误 任何想法都值得赞赏 我的目录看起来像 /Box Tracking --/res ----amazon_box.jpg --main.py --gui.py gui.py中的相关代码: 将tkinter作为tk导入 进口干管 导入回溯 导入时间 随机输入 #使用这些LIB,因为基本tkinter只能打开gif和其他模糊格式 #必须安装枕头而不是PIL 从PIL导

我想为我的.exe添加一个图像。我正在按照上面列出的方向走。它说构建在终端中成功完成。当我运行.exe时,它会在程序中标记一个致命错误

任何想法都值得赞赏

我的目录看起来像

/Box Tracking
--/res
----amazon_box.jpg
--main.py
--gui.py
gui.py中的相关代码:

将tkinter作为tk导入
进口干管
导入回溯
导入时间
随机输入
#使用这些LIB,因为基本tkinter只能打开gif和其他模糊格式
#必须安装枕头而不是PIL
从PIL导入ImageTk,图像
从笑话到笑话
导入sys,os#这些将允许我在发行版中捆绑映像
def资源路径(相对路径):
'''
见附注
:param relative_path:path我将使用| string |'res/amazon box.jpg'
:return:string | path
'''
如果hasattr(sys,“\u MEIPASS”):
返回os.path.join(系统路径、相对路径)
返回os.path.join(os.path.abspath(“.”),相对路径)
root=tk.tk()#创建GUI实例
几何体(“1000x1000”)#使GUI变大,以便用户可以看到它
root.title('Box Tracking')#name GUI
#添加背景以便用户可以看到它
bg_image=image.open(资源路径('res/amazonbox.jpg'))
tk_bg_image=ImageTk.PhotoImage(bg_image)
bg=tk.Label(根,图像=tk\u bg\u图像)
背景位置(x=0,y=0,relwidth=1,relheight=1)
我用于生成.exe的代码段:
pyinstaller--onefile--windowed--adddata res/amazon box.jpg--命名为“Box Tracking 3.1.5”gui.py

可能有更好的方法,但我是这样做的:

\res\
目录中,我会有
amazon\u box.py
和一个空的
\uuuuu init\uuuuu.py

可以使用以下小脚本将图像转换为base64:

import base64
with open("amazon_box.jpg", "rb") as image:
    b = base64.b64encode(image.read())
with open("amazon_box.py", "w") as write_file:
    write_file.write("def photo(): return (" + str(b) + ")"
amazon_box.py
中,它将具有(由上述脚本自动创建):

之后,图像可以在主脚本中使用
PhotoImage(data=)

显然,我没有用你的图像测试它,但这就是我过去将图像包装到一个文件中的方式,你可以在一个
.py
文件中有多个图像,因此它实际上可以是一个非常干净的最终结果

编辑:

由于此时它只是PyInstaller的另一个模块,因此我的构建命令是标准的:
PyInstaller.exe--windowed--onefile“path\to\main\py\file”


我通常只是在命令提示符下运行PyInstaller。

@ChristinaZhou实际上,我很高兴我进一步回顾了这一点,我发现,如果编码正确,
tempfile
是不需要的,并且
tk.PhotoImage
可以直接加载大多数base64编码的图像。
def photo(): return (image_as_base64)
import tkinter as tk
from res import amazon_box

root = tk.Tk()  # create GUI instance

root.geometry("1000x1000") # make GUI big so that users can see it
root.title('Box Tracking') # name GUI

# add a background so users can see it
tk_bg_image = tk.PhotoImage(data = amazon_box.photo())
bg = tk.Label(root,image=tk_bg_image)
bg.place(x=0,y=0,relwidth=1,relheight=1)