简体   繁体   English

不会为使用pyinstaller编译的tkinter程序加载图像

[英]Image won't load for tkinter program compiled with pyinstaller

I have a tkinter program that includes a .png image. 我有一个包含.png图像的tkinter程序。 I have compiled it using pyinstaller and the --onefile option so I have to access the image in a temporary location. 我已经使用pyinstaller和--onefile选项对其进行了编译,因此我必须在一个临时位置访问该映像。 This is the code I am using: 这是我正在使用的代码:

def resource_path(relative_path):
    try:
        base_path = sys._MEIPASS
    except Exception as e:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)
title = PhotoImage(file=resource_path("xgol.png"))

Here is my .spec file: 这是我的.spec文件:

# -*- mode: python -*-

block_cipher = None


a = Analysis(['XGols.py'],
             pathex=['C:\\Users\\Sam\\OneDrive\\Computing\\Python Projects\\FootballPredict'],
             binaries=[],
             datas=[('Ball.ico', 'Ball.ico'), ('xgol.png', 'xgol.png')],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='XGols',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=True , icon='Ball.ico')

To compile the program I use: 要编译程序,我使用:

pyinstaller --onefile XGols.spec

This is the error that I get when I run the executable 这是我运行可执行文件时遇到的错误

_tkinter.TclError: couldn't open "C:\Users\Sam\AppData\Local\Temp\_MEI61842\xgol.png": permission denied

I have tried running as administrator. 我尝试以管理员身份运行。

In your .spec file, additional data files should be listed like so: 在您的.spec文件中,其他数据文件应如下列出:

datas=[('Ball.ico', '.'), ('xgol.png', '.')]

From the documentation : 文档中

Each tuple has two values, both of which must be strings: 每个元组都有两个值,两个值都必须是字符串:

  • The first string specifies the file or files as they are in this system now. 第一个字符串指定文件或它们现在在此系统中的位置。
  • The second specifies the name of the folder to contain the files at run-time. 第二个字母指定运行时包含文件的文件夹的名称。

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

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