我在堆栈上找到了一些有关此信息的信息,并且一直在尝试,但对我不起作用。我需要社区的帮助:)
标题所建议的,我想在我的tkinter窗口中添加 *.ICO。问题在于,当使用Pyinstaller创建单个EXE文件时,这不是很简单。因此,下面正是我从堆栈溢出上收集的信息中实现的目的。在64位机器上使用Python 3.5。
我的脚本的名称是calculator.pyICO的名称是计算器。ICO
1。我将以下代码行添加到我的python脚本:
import os
import sys
datafile = "calculator.ico"
if not hasattr(sys, "frozen"):
datafile = os.path.join(os.path.dirname(__file__), datafile)
else:
datafile = os.path.join(sys.prefix, datafile)
window = tkinter.Tk()
window.iconbitmap(default=datafile)
2a。然后,我使用以下命令使用Pyinstaller构建了单个EXE文件:
pyinstaller -w -F -i "C:PythonProjectsCalccalculator.ico" calculator.py
2B。我还尝试使用以下命令构建单个EXE文件 查看是否有帮助:
pyinstaller --onefile --windowed --icon=calculator.ico calculator.py
3。在Pyinstaller构建了单个EXE文件之后,我然后在 *.spec文件中添加了一些内容 - 下面是完整的 *.spec文件,其中添加了内容:
# -*- mode: python -*-
block_cipher = None
a = Analysis(['calculator.py'],
pathex=['C:\PythonProjects\calc'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
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 + [('caclulator.ico', 'C:\PythonProjects\calc\calculator.ico', 'DATA')],
a.zipfiles,
a.datas,
name='calculator',
debug=False,
strip=False,
upx=True,
console=False , icon='calculator.ico')
我将以下内容添加到exe = exe:
+ [('caclulator.ico', 'C:\PythonProjects\calc\calculator.ico', 'DATA')]
将上述内容添加到规格文件后,我运行以下命令。
pyinstaller calculator.spec
重建EXE后,我运行EXE文件,然后收到以下错误消息:
- 无法执行脚本计算器
预先感谢!
您做了正确的事情。但是您只是错过了最后一步。在您的代码中添加以下几行,它应该起作用。
def resource_path(relative_path):
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
然后调用此。
window.iconbitmap(default=resource_path(datafile))
您需要做的就是将.ICO映像放置在.py文件的根文件夹中,然后使用Pyinstaller从您的.py文件创建.exe时,您可以键入以下内容" pyinstaller - honefile -icon = my.ico my.py",指定图标的名称,它必须是.ico文件,如果不起作用。
要删除窗口中的默认羽毛图标,将图标移动到.py文件的根文件夹并使用iconbitmap方法并指定图标的名称,例如...root = tk.tk()root.iconbitmap(" myicon.ico")
遇到了相同的问题,以以下方式解决:
- 图标参数不会将ICO文件添加到exe包装中,您需要通过-add-data参数添加它:
pyInstaller ... --add-data "icon.ico;." script.py
现在,它将在运行EXE文件时出现在临时_meipassxxxxx文件夹中,您可以按照Satyendra Sahani建议的