无法为用pyins编译的tkinter程序加载图像

2024-09-30 20:25:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个tkinter程序,包括一个.png图像。我已经使用pyinstaller和--onefile选项编译了它,所以我必须在临时位置访问图像。这是我正在使用的代码:

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"))

这是我的.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')

要编译我使用的程序:

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

我试过当管理员。你知道吗


Tags: path图像程序falsebasepngtkinterblock
1条回答
网友
1楼 · 发布于 2024-09-30 20:25:29

.spec文件中,其他数据文件应如下所示:

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

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.

相关问题 更多 >