使用 pyinstaller 编译为 .exe 后 PyQt5 应用程序缺少图标
Icons missing from PyQt5 app after compiling to .exe using pyinstaller
已解决 - 解决方案作为答案发布,感谢大家的帮助。
使用 PyQt5
将我的 python 应用程序编译为可执行文件后,我的 GUI 中包含的图标被删除/不显示。
具体来说 QIcon
个实例添加到我的 Window(QMainWindow)
class 使用 self.setWindowIcon(QtGui.QIcon(fpath))
和 QPixmap(f2path)
通过 label.setPixmap(myPixmap)
嵌入 QLabel
.
我试图在该论坛上搜索可能的解决方案,但找不到解决问题的帖子。
我已尝试按照此处的建议设置绝对文件路径 Bundling data files with PyInstaller (--onefile) and here Missing button icons in pyinstaller
不知道从哪里开始检查问题,使用 pyinstaller
编译时没有错误,并且作为 python 脚本运行良好。
pyinstaller -w -F MY_GUI.py
提前致谢!
示例:
import sys
import os
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_path, relative_path)
import sys
import resource_path # code taken from links above
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QMainWindow
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.title = "MyProg"
self.top = 400
self.left = 400
self.width = 680
self.height = 540
icon_path = resource_path("icon.png")
self.setWindowIcon(QtGui.QIcon(icon_path))
self.InitUI()
def InitUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.top, self.left, self.width, self.height)
self.show()
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
解决方案是专门将图像文件添加到 .spec
文件,然后使用
生成 .exe
文件
$> pyinstaller myGUI.spec
这里是 .spec
文件的相关部分:
a = Analysis(['myGUI.py'],
...,
datas = [('myIcon.png', '.')],
...)
已解决 - 解决方案作为答案发布,感谢大家的帮助。
使用 PyQt5
将我的 python 应用程序编译为可执行文件后,我的 GUI 中包含的图标被删除/不显示。
具体来说 QIcon
个实例添加到我的 Window(QMainWindow)
class 使用 self.setWindowIcon(QtGui.QIcon(fpath))
和 QPixmap(f2path)
通过 label.setPixmap(myPixmap)
嵌入 QLabel
.
我试图在该论坛上搜索可能的解决方案,但找不到解决问题的帖子。 我已尝试按照此处的建议设置绝对文件路径 Bundling data files with PyInstaller (--onefile) and here Missing button icons in pyinstaller
不知道从哪里开始检查问题,使用 pyinstaller
编译时没有错误,并且作为 python 脚本运行良好。
pyinstaller -w -F MY_GUI.py
提前致谢!
示例:
import sys
import os
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_path, relative_path)
import sys
import resource_path # code taken from links above
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QMainWindow
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.title = "MyProg"
self.top = 400
self.left = 400
self.width = 680
self.height = 540
icon_path = resource_path("icon.png")
self.setWindowIcon(QtGui.QIcon(icon_path))
self.InitUI()
def InitUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.top, self.left, self.width, self.height)
self.show()
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
解决方案是专门将图像文件添加到 .spec
文件,然后使用
.exe
文件
$> pyinstaller myGUI.spec
这里是 .spec
文件的相关部分:
a = Analysis(['myGUI.py'],
...,
datas = [('myIcon.png', '.')],
...)