我已经从 python tkinter GUI 成功创建了一个包含图像的 EXE 文件。看下面的代码:
lblLogo=Label(main)
lblLogo.grid(row=3,column=11,rowspan=4)
try:
filelocation="C:/Documents/Python/ScreenPartNumberProgram/"
KasonLogo=PhotoImage(file=filelocation+"KasonLogo.png")
KasonLogo=KasonLogo.zoom(25,20)
KasonLogo=KasonLogo.subsample(50,50)
lblLogo.config(image=KasonLogo)
icon=PhotoImage(file=filelocation+"KasonLogo.png")
main.tk.call('wm','iconphoto',main._w,icon)
except:
print("warning: could not load photos")
lblLogo.config(text="[KasonLogo.png]")
Run Code Online (Sandbox Code Playgroud)
exe 文件在我的计算机上打开并完美运行。我使用此命令来获取 .exe:
pyinstaller --onefile --noconsole myscript.py
Run Code Online (Sandbox Code Playgroud)
唯一的问题是该文件要求图像位于同一文件夹中,否则不会显示在标签上。如何在不需要外部文件的情况下将图像捆绑到 EXE 中?
提前致谢
编辑:我查看了提供的链接,做了更多研究,但仍然没有解决问题。仅当图像位于同一文件夹中时该程序才有效。我真的很想让它在不需要外部图像文件的情况下工作。resources_path 技术似乎也不适合我......
我还尝试修改我的代码以使用 PIL 的 ImageTk 和同样的问题。仅当外部图像文件位于其文件夹中时,程序才会加载图像。