【发布时间】:2018-10-17 17:14:34
【问题描述】:
我想在使用 cx_Freeze 冻结基线后在 PyQt 窗口中显示自定义图标。当从 IDE(对我来说是 Spyder)执行解冻脚本时,该图标显示正常。我正在使用 PyQt5、Python 3.6 和 Windows 10。这是我的 Python 脚本 (IconTest.py),它创建一个主窗口并显示图标的路径以及该路径是否存在。图标文件需要和IconTest.py在同一目录下:
import sys, os
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtGui import QIcon
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(200, 300, 600, 100)
if getattr(sys, 'frozen', False): #If frozen with cx_Freeze
self.homePath = os.path.dirname(sys.executable)
else: # Otherwise, if running as a script (e.g., within Spyder)
self.homePath = os.path.dirname(__file__)
self.iconFileName = os.path.join(self.homePath, 'myIcon.ico')
self.setWindowIcon(QIcon(self.iconFileName))
self.setWindowTitle('Icon')
self.label1 = QLabel(self)
self.label2 = QLabel(self)
self.label1.move(10, 20)
self.label2.move(10, 40)
self.label1.setText("Path to icon file: " + str(self.iconFileName))
self.label2.setText("Does file exit? " + str(os.path.exists(self.iconFileName)))
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
这是我在 Spyder(解冻)中运行脚本时的结果。如您所见,显示的图标类似于秒表:
这是我用于创建冻结基线的 setup.py:
from cx_Freeze import setup, Executable
import os, sys
exeDir = os.path.dirname(sys.executable)
platformsPath = os.path.join(exeDir, "Library\\Plugins\\Platforms\\")
iconPath = os.path.join(os.path.dirname(__file__), "myIcon.ico")
exe=Executable(script="IconTest.py", base = "Win32GUI", icon = iconPath)
includes=[iconPath, platformsPath]
excludes=[]
packages=[]
setup(
version = "0.1",
description = "My Icon Demo",
options = {'build_exe': {'excludes':excludes,'packages':packages,'include_files':includes}},
executables = [exe]
)
这是我运行冻结脚本时的结果(build 目录中的可执行文件)。如您所见,秒表图标已替换为通用窗口图标:
建议?
【问题讨论】:
-
您必须改进最初的问题或至少删除它,而不是创建多个具有相同问题的帖子
-
我标记了另一个问题,供版主删除。我无法删除它,因为它已被回答。问题和回答的方向是错误的,所以我觉得有必要用最少、完整和可行的代码完全重写问题。
-
图标是否真的被复制到了构建文件夹中?
-
@slalomchip 我已删除我对您其他问题的回答,以便您将其删除。
-
@Jannick - 是的,图标文件实际上已复制到
build文件夹。否则,窗口中的第二行将是False而不是True
标签: python pyqt icons pyqt5 cx-freeze