【问题标题】:python pyinstaller bundle image in GUI to onefile EXE?GUI中的python pyinstaller捆绑图像到onefile EXE?
【发布时间】: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

标签: python tkinter exe photo


【解决方案1】:

圣牛!经过几天的头痛后,我终于找到了一种可行的方法......这是我在不需要外部图像文件的情况下让程序工作的方法:

第一步:对图片文件进行编码(注意必须是GIF,可以用paint转换):

import base64
with open("MyPicture.gif", "rb") as image_file:
    encoded_string = base64.b64encode(image_file.read())
print(encoded_string)#print string to copy it (see step 2)

第 2 步:下一步是将字符串复制并粘贴到单独的 myimages.py 文件中,然后 将其存储为变量。例如:

imageString = b'R0lGODlhyADIAPcAAAA .....blah blah really long string.......'

第三步:然后将 myimages.py 导入主程序并调用变量

from myimages import *
pic=imageString#GIF decoded to string. imageString from myimages.py
render = PhotoImage(data=pic)
myLabel.config(image=render)

第 4 步:在规范文件中:将 myimages.py 包含为数据

# -*- mode: python -*-

block_cipher = None

#instructions: pyinstaller --onefile --noconsole myProgram.spec

file_name = 'myProgram'
add_files=[('myimages.py','module')]

a = Analysis([file_name+'.py'],
             pathex=['C:\\Program Files (x86)\\Python36-32\\Scripts'],
             binaries=[],
             datas=add_files,
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=exclude_list,
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name=file_name,
          debug=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=False )

第五步:终于可以编译成EXE了:

pyinstaller --onefile --noconsole myProgram.spec

SIGH 如果有人有更好的解决方案,请在此处发布。在那之前,我很高兴有些东西能奏效......

【讨论】:

  • 我遇到了同样的问题。您的 myLabel 变量引用是什么?那是 tkinter 标签吗?
  • 这是一个很酷的解决方案。我觉得必须有一种更合法的方式来做到这一点
【解决方案2】:

当我想要包含一个 .ico 文件用作我的 tkinter 应用程序的图标时,我遇到了同样的问题。这是我如何让它工作的:

  1. 使用 PyInstaller 构建 .exe 文件时,我指定了 --add-data 选项以及我的 .ico 的完整路径。

  2. 1234563 /p>

在主循环开始之前,使用“tempfile”获取临时文件目录并使用“glob”检查文件模式匹配:

tempdir = tempfile.gettempdir()
icon_path = tempdir + '\\' + 'next_subdirectory_pattern' + '\\' + '*.ico'
icon_matches = glob.glob(icon_path)
if len(icon_matches) > 0:
    logo = icon_matches[0]
else:
    logo = ''
tk.Tk.iconbitmap(self, default=logo)

可能不是最有说服力的方法,但它确实有效。

【讨论】:

    【解决方案3】:

    我是这样做的……

    要将图像与可执行文件捆绑:

    使用以下命令将您的图标保存到名为“icon”的文件夹中,该文件夹将在 pyinstaller 在运行时生成的“C:\Users\AppData\Local\Temp_MEI123456”文件夹中生成:

    --add-data=C:\absolute\path\to\your\ICON.ico;icon
    

    我的完整命令行,我在 python 终端中输入的字符串(我正在使用 pycharm IDE)生成我的 EXE,如下所示:

    pyinstaller -w -F -i=C:\absolute\path\to\your\ICON.ico --add-data=C:\absolute\path\to\your\ICON.ico;icon -n=MyEXEsNameV1 main.py
    

    (对于调试,省略 -w 并添加 --debug=all 会很有帮助)

    文档参考:https://pyinstaller.readthedocs.io/en/stable/usage.html#options-group-what-to-bundle-where-to-search

    在我的 python 脚本 main.py 中访问图像:

    我使用以下代码来设置 tk 窗口:

    import tkinter as tk
    from os import path
    
        # main widget
        self.root = tk.Tk(master)
        self.root.title('Window Title Bar Name V1.0')
        self.root.minsize(1234, 1234)
    
        # get icon
        try:
            # Get the absolute path of the temp directory
            path_to_icon = path.abspath(path.join(path.dirname(__file__), 'icon/GAUGE-ICON.ico')) 
            # set the icon of the tk window
            self.root.iconbitmap(path_to_icon)
        except:
            pass
    

    文档参考:https://pyinstaller.readthedocs.io/en/stable/runtime-information.html

    【讨论】:

      【解决方案4】:

      除了接受的答案我想添加修改版本:

      from PIL import Image, ImageTk
      
      from res import *
      #res is a module with your base64 image string and in my case variable is icon_base64
      from base64 import b64decode
      
      pic_as_bytes=ImageTk.BytesIO(icon_base64)
      my_image=Image.open(pic_as_bytes)
      

      请注意,此代码可以在 Python3 中使用,但在 Python2 中无法使用,因为它需要不同的方法和更多的转换(下面的链接正是 Python2 的示例)。

      更多信息请参考https://github.com/Ariel-MN/Tkinter_base64_Images/blob/master/tkinter_base64_image-ico.py

      【讨论】:

        猜你喜欢
        • 2012-12-06
        • 1970-01-01
        • 2011-12-02
        • 2020-12-17
        • 2019-01-04
        相关资源
        最近更新 更多