【发布时间】:2018-01-07 04:19:09
【问题描述】:
我已经从 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]")
exe 文件在我的计算机上打开并完美运行。我用这个命令来获取.exe:
pyinstaller --onefile --noconsole myscript.py
唯一的问题是此文件要求图像位于同一文件夹中,否则它不会显示在标签上。如何在不需要外部文件的情况下将图像捆绑到 EXE 中?
提前致谢
编辑: 我查看了提供的链接,进行了更多研究,但仍未解决问题。该程序仅在图像位于同一文件夹中时才有效。我真的很想让它在不需要外部图像文件的情况下工作。 resource_path 技术似乎对我也不起作用......
我还尝试修改我的代码以使用 PIL 的 ImageTk 和相同的问题。程序仅在外部图像文件在其所在文件夹中时才加载图像。
【问题讨论】:
-
您好,感谢您的回复。我知道该程序需要外部图像文件,但有没有办法不需要外部文件?我觉得一定有办法把图片打包进EXE