使用PyIns后不显示样式表的QCombobox图标集

2024-04-26 03:05:38 发布

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

我正在编写一个Python程序,它使用PyQt作为UI。当我用python运行它时,会像预期的那样显示QComboBox图标。但是,在我使用PyInstaller编译它并运行程序之后,我得到了一个错误(见下文),然后图标就不显示了。你知道吗

我尝试过的

  • 您将在我的spec文件中看到,我将文件路径放在了“datas”中。你知道吗
  • 您将在我的代码中看到,我有必要的资源路径函数。在更大的程序中,我成功地用它连接了一个SQLite数据库。你知道吗

奇怪的问题

图标是样式表中QComboBox::down-arrow选择器中的图像。你知道吗

  1. 当我提供绝对路径(如代码中所示)时,图标可用于python,但在使用PyInstaller编译时不起作用
  2. 当我使用resource\u path()提供路径时,图标根本不起作用。你知道吗

如何在使用PyInstaller后显示此图标?你知道吗

错误

qt.svg: Cannot open file '/Users/user.name/icons/wd-icon-prompts.svg', because: No such file or directory

代码

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
# QT Layout Doc: https://doc.qt.io/qt-5/layout.html


# Subclass QMainWindow to customise your application's main window
class MainWindow(QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        self.setWindowTitle("Example")

        # label = QLabel("This is a PyQt5 window!")
        combo = QComboBox()
        combo.addItems(['Item 1', 'Item 2', 'Item 3'])


        self.setCentralWidget(combo)

        styleSheet = """
        QComboBox {
            background-color: #fff;
            border: 1px solid #ced3d9;
            border-radius: 3px;
            color: #333333;
            font: 14px;
            height: 36px;
            padding: 1px 0px 1px 3px;
            padding: ;
            margin: 50px;
            min-width: 300px;
            selection-background-color: #e8ebed;
        }
        QComboBox:editable {
            background: #fff;
        }
        QComboBox::drop-down {
            background: #fff;
            border: 0;
            subcontrol-origin: padding;
            subcontrol-position: right;
            width: 25px;
        }
        QComboBox::down-arrow {
            image: url(icons/wd-icon-prompts.svg);
            width: 26px;
            height: 26px;
            padding: 25px;
        }
        QComboBox::down-arrow:active {
        }
        QComboBox QAbstractItemView {
            background: white;
        }
        """

        self.setStyleSheet(styleSheet)


    def resource_path(self,relative_path):
        """ Get absolute path to resource, works for dev and for PyInstaller """
        try:
            # PyInstaller creates a temp folder and stores path in _MEIPASS
            base_path = sys._MEIPASS
        except Exception:
            base_path = os.environ.get("_MEIPASS2",os.path.abspath("."))
        return os.path.join(base_path, relative_path)


app = QApplication(sys.argv)

window = MainWindow()
window.show()

app.exec_()

规范文件

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None


a = Analysis(['codeSample.py'],
             pathex=['/Users/user.name/Desktop/SoapDayCode copy'],
             binaries=[],
             datas=[('icons/wd-icon-prompts.svg', 'icons')],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name='codeSample',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               upx_exclude=[],
               name='codeSample')

编辑:

问题1)如何运行pyinstaller?你知道吗

  1. 第一次:pyinstaller mycode.py
  2. 然后编辑spec文件并将svg文件添加到数据中
  3. 从现在开始排除故障时:pyinstaller mycode.spec

问题2)如何运行.exe?你知道吗

  • 编译之后,我导航到/dist/mycode/并找到名为mycode的exe。我双击mycode

问题3)在.exe旁边有文件吗?如果有,那是什么?你知道吗

是的,有很多,因为我没有使用onefile选项。这是一张照片

picture of file structure


Tags: 文件pathnamesvgselffalseexe图标