简体   繁体   English

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

[英]Bundle data for pyinstaller error (--onefile)

I want to include an image for my.exe.我想为 my.exe 包含一个图像。 I'm following the directions as listed here .我正在按照此处列出的说明进行操作。 It says the build completes successfully in terminal.它说构建在终端中成功完成。 When I run the.exe it marks a fatal error in the program.当我运行 .exe 时,它标志着程序中的一个致命错误。

Any ideas appreciated.任何想法表示赞赏。

My directory looks like我的目录看起来像

/Box Tracking
--/res
----amazon_box.jpg
--main.py
--gui.py

Related code from gui.py:来自 gui.py 的相关代码:

import tkinter as tk
import main
import traceback
import time
import random
# use these libs since base tkinter can only open gif and other obscure formats
# must install Pillow rather than PIL
from PIL import ImageTk, Image
from jokes import jokes
import sys, os # these will allow me to bundle the image in the distro

def resource_path(relative_path):
    '''
    See notes
    :param relative_path: path I will use | string | 'res/amazon-box.jpg'
    :return: string | path
    '''
    if hasattr(sys, '_MEIPASS'):
        return os.path.join(sys._MEIPASS, relative_path)
    return os.path.join(os.path.abspath("."), relative_path)

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
bg_image = Image.open(resource_path('res/amazon-box.jpg'))
tk_bg_image = ImageTk.PhotoImage(bg_image)
bg = tk.Label(root,image=tk_bg_image)
bg.place(x=0,y=0,relwidth=1,relheight=1)

Snippet I use to build the.exe: pyinstaller --onefile --windowed --add-data res/amazon-box.jpg;. --name "Box Tracking 3.1.5" gui.py我用来构建.exe 的片段: pyinstaller --onefile --windowed --add-data res/amazon-box.jpg;. --name "Box Tracking 3.1.5" gui.py pyinstaller --onefile --windowed --add-data res/amazon-box.jpg;. --name "Box Tracking 3.1.5" gui.py

There may be a better way, but I do it this way:可能有更好的方法,但我这样做:

In the \res\ directory I'd have amazon_box.py and an empty __init__.py\res\目录中,我有amazon_box.py和一个空的__init__.py

You can convert an image to a base64 using a small script such as:您可以使用以下小脚本将图像转换为 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) + ")"

in amazon_box.py it will have (automatically created by the above script):amazon_box.py中它将具有(由上述脚本自动创建):

def photo(): return (image_as_base64)

After that the image can be used in the main script with PhotoImage(data = ) :之后,图像可以通过PhotoImage(data = )在主脚本中使用:

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)

Obviously I haven't tested it with your image, but this is how I've wrapped images into onefiles in the past, and you can have multiple images in the one .py file, so it actually can be a pretty clean end result.显然我没有用你的图像测试它,但这是我过去将图像包装到一个文件中的方式,你可以在一个.py文件中包含多个图像,所以它实际上可以是一个非常干净的最终结果。

Edit:编辑:

Since it's just another module to PyInstaller at this point, my build command is the standard: pyinstaller.exe --windowed --onefile "path\to\main\py\file"由于此时它只是 PyInstaller 的另一个模块,因此我的构建命令是标准的: pyinstaller.exe --windowed --onefile "path\to\main\py\file"

I usually just run PyInstaller from command prompt.我通常只是从命令提示符运行 PyInstaller。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM