在 Windows10 上使用带有诗歌的 pyinstaller 时无法找到 tkinter 图标文件

问题描述 投票:0回答:1

我正在使用这个答案来查找我的图标文件。我的项目是这样组织的

.
│   main.spec
│   poetry.lock
│   pyproject.toml
│   README.md
│
├───my_app
│   │   pyinstaller.py
│   │   __init__.py
│   │
│   └───src
│       │   main.py
│       │
│       └───images
│               icon.png
│
└───dist
        my_app.exe

我的代码:

# main.py
import sys
import os
import tkinter as tk
from pathlib import Path

def main() -> None:
    root = tk.Tk()
    icon_file = resolve_path('images/icon.png')
    root.geometry('600x200')
    root.columnconfigure(0, weight=1)
    display_info = tk.StringVar()
    display = tk.Entry(root, textvariable= display_info)
    display.grid(sticky=tk.EW)
    info = f'{Path(icon_file).is_file()} - {icon_file}'
    display_info.set(info)
    # root.iconphoto(False, tk.PhotoImage(file=icon_file))
    root.mainloop()

def resolve_path(path) -> str:
    if getattr(sys, 'frozen', False):
        return os.path.abspath(Path(sys._MEIPASS, path))
    return os.path.abspath(Path(Path(__file__).parent, path))

if __name__ == '__main__':
    main()

运行脚本时得到的输出是

True - C:\Users\fred\projects\my_app\my_app\src\images\icon.png

但是当我构建 exe 并运行它时,输出是

False - C:\Users\fred\AppData\Local\Temp\_MEI163682\images\icon.png

所以它没有找到该文件

应该放在哪里?

python windows tkinter pyinstaller python-poetry
1个回答
0
投票

该文件需要添加到由 pyinstaller 创建的捆绑包中。

为此,您需要 (i) 使用

--add-data

 命令,或 (ii) 在 datas
 文件的 
Analysis
 类中添加 
main.spec
 属性。

规格文件看起来像这样:

a = Analysis(... datas=[ ('src\images\icon.png', 'images') ], ... )
    
© www.soinside.com 2019 - 2024. All rights reserved.