如何将映像添加到onefile pyinstaller可执行文件中?

时间:2020-03-14 14:00:21

标签: python tkinter pyinstaller

我的pyinstaller可执行文件遇到问题,启动它时出现此错误:

enter image description here

问题是由于exe无法找到映像文件'icon.ico',因为pyinstaller没有将文件打包在可执行文件中。我要问的是如何将图标打包在EXE文件中,以及打包后该图标文件的目录是什么。该图像文件被用作Tkinter GUI的图标。

这是Tkinter应用程序的代码:

app = Tk()
app.title('MagnetMagnet - RARBG Scraper')
app.iconbitmap(r'icon.ico')
app.geometry('500x225')

app.mainloop()

2 个答案:

答案 0 :(得分:2)

一种简单的方法是保存图像的Bytes,并在打开图像时将图片保存在PC中,然后使用app.iconbitmap(r'icon.ico')

首先,使用open获取图像字节:

with open('icon.ico','rb') as f:
    ImageBytes = f.read()
print(ImageBytes)
# b'xxxxxxxxxxxxxxxxxx'

然后您的所有代码应为:

ImageBytes = b'xxxxxxxxxxxxxxxxxx'
with open('icon.ico','wb') as f:
    f.write(ImageBytes)
app = Tk()
app.title('MagnetMagnet - RARBG Scraper')
app.iconbitmap(r'icon.ico')
app.geometry('500x225')
app.mainloop()

打开此exe文件时,它将生成一个新的ico图像,您可以将其删除。下次打开该文件时,它将再次生成一个新图像。

答案 1 :(得分:1)

我认为您不必在python文件中提供图标。 而是在终端机(Windows)中输入pyinstaller -i iconfile pythonfile