从pyqtins.exe编译到pyqtins后丢失了图标

2024-04-20 08:52:45 发布

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

已解决-答案已发布,谢谢所有人的帮助。

使用PyQt5将python应用程序编译为可执行文件后,GUI中包含的图标将被删除/不显示。 特别是使用self.setWindowIcon(QtGui.QIcon(fpath))和通过label.setPixmap(myPixmap)嵌入QLabel中的QIcon实例添加到我的Window(QMainWindow)类中。在

我试着在这个论坛上搜索可能的解决方案,但找不到解决问题的线索。 我已经尝试将绝对文件路径设置为此处Bundling data files with PyInstaller (--onefile)和此处Missing button icons in pyinstaller

不知道从何处开始检查问题,使用pyinstaller编译时没有错误,而且它作为python脚本运行良好。在

pyinstaller -w -F MY_GUI.py

提前谢谢!在



示例:

^{pr2}$
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())

Tags: pathfromimportselfsysguiwindowresource
1条回答
网友
1楼 · 发布于 2024-04-20 08:52:45

解决方案是专门将图像文件添加到.spec文件中,然后使用

$> pyinstaller myGUI.spec

以下是.spec文件的相关部分:

^{pr2}$

相关问题 更多 >