RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1403609
Accepted
Masik
Masik
Asked:2022-06-25 16:44:26 +0000 UTC2022-06-25 16:44:26 +0000 UTC 2022-06-25 16:44:26 +0000 UTC

图标不会从 .qrc 导入到 .py

  • 772

我在Qt Designer中创建了一个界面,并将资源文件resource.qrc用于应用程序中的图标。将Designer中的.ui
保存为.py文件, 然后使用以下命令将resource.qrc转换为rc_resource.py:

pyside6-rcc resource.qrc -o rc_resource.py

我必须马上说PyQt5 也有同样的问题。

但是,当我尝试Main从文件运行应用程序时,出现错误:

“ModuleNotFoundError:没有名为‘rc_resource’的模块”。

文件中有一个导入ui_MainWindow,但显然出现了问题。

如果我删除导入rc_resource并直接在它们存储的文件夹中替换图标的路径,而不是在 中qrc,图标仍然不可见。
ui_MainWindow并rc_resource存储在同一个文件夹中。

帮助解决问题。 https://github.com/Masik84/my_prog

资源.qrc

<RCC>
  <qresource prefix="icons">
    <file>Icons/minus.png</file>
    <file>Icons/alignleft2.png</file>
    ...
    <file>Icons/x.png</file>
  </qresource>
</RCC>

ui_MainWindow.py

from PySide6.QtWidgets import (QApplication, QFrame, QHBoxLayout, QMainWindow,
    QPushButton, QSizePolicy, QVBoxLayout, QWidget)

import rc_resource

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        if not MainWindow.objectName():
         ...

主文件

import sys

from PySide6 import QtCore, QtGui
from PySide6.QtGui import QColor
from PySide6.QtWidgets import QMainWindow, QDialog, QApplication, QGraphicsDropShadowEffect

from screens.ui_MainWindow import *

class Welcome_Screen(QMainWindow):
    def __init__(self):
        super(Welcome_Screen,self).__init__()
        self.animation = None
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Welcome_Screen()
    sys.exit(app.exec_())
python pyqt5
  • 1 1 个回答
  • 75 Views

1 个回答

  • Voted
  1. Best Answer
    S. Nick
    2022-06-29T21:32:10Z2022-06-29T21:32:10Z

    在ui_MainWindow.py模块中替换

    import rc_resource
    

    在

    import screens.rc_resource
    

    主文件

    import sys
    '''
    from PySide6 import QtCore, QtGui
    from PySide6.QtGui import QColor
    from PySide6.QtWidgets import QMainWindow, QDialog, QApplication, QGraphicsDropShadowEffect
    '''
    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.Qt import *
    
    from screens.ui_MainWindow import Ui_MainWindow
    
    
    class Welcome_Screen(QMainWindow):
        def __init__(self):
            super(Welcome_Screen, self).__init__()
            
            self.ui = Ui_MainWindow()
            self.ui.setupUi(self)
            
            self.animation = None
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        window = Welcome_Screen()
        window.show()
        sys.exit(app.exec_())
    

    ui_MainWindow.py

    from PyQt5 import QtCore, QtGui, QtWidgets
    
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(835, 572)
            MainWindow.setAutoFillBackground(True)
            MainWindow.setStyleSheet("")
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setAutoFillBackground(False)
            self.centralwidget.setStyleSheet("")
            self.centralwidget.setObjectName("centralwidget")
            self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
            self.verticalLayout.setContentsMargins(0, 0, 0, 0)
            self.verticalLayout.setSpacing(0)
            self.verticalLayout.setObjectName("verticalLayout")
            self.header = QtWidgets.QFrame(self.centralwidget)
            self.header.setMinimumSize(QtCore.QSize(0, 0))
            self.header.setMaximumSize(QtCore.QSize(16777215, 50))
            self.header.setAutoFillBackground(False)
            self.header.setStyleSheet(".QFrame#header{\n"
    "background-color: qlineargradient(spread:pad, x1:0.1, y1:0.215909, x2:0.959, y2:0.909091, stop:0 rgba(52, 146, 35, 255), stop:1 rgba(195, 244, 101, 255));\n"
    "}")
            self.header.setFrameShape(QtWidgets.QFrame.NoFrame)
            self.header.setFrameShadow(QtWidgets.QFrame.Raised)
            self.header.setLineWidth(0)
            self.header.setObjectName("header")
            self.hboxlayout = QtWidgets.QHBoxLayout(self.header)
            self.hboxlayout.setContentsMargins(0, 0, 0, 0)
            self.hboxlayout.setSpacing(0)
            self.hboxlayout.setObjectName("hboxlayout")
            self.top_left_menu = QtWidgets.QFrame(self.header)
            self.top_left_menu.setMinimumSize(QtCore.QSize(0, 0))
            self.top_left_menu.setMaximumSize(QtCore.QSize(45, 50))
            self.top_left_menu.setStyleSheet("QFrame#top_left_menu{\n"
    "background-color: qlineargradient(spread:pad, x1:0, y1:0.506, x2:1, y2:0.5, stop:0 rgba(52, 146, 35, 255), stop:1 rgba(195, 244, 101, 255));\n"
    "}\n"
    "\n"
    "QPushButton{\n"
    "    border-radius: 5px;\n"
    "}\n"
    "\n"
    "QPushButton:hover {\n"
    "    background-color: rgb(93, 183, 42);\n"
    "}")
            self.top_left_menu.setFrameShape(QtWidgets.QFrame.StyledPanel)
            self.top_left_menu.setFrameShadow(QtWidgets.QFrame.Raised)
            self.top_left_menu.setObjectName("top_left_menu")
            self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.top_left_menu)
            self.horizontalLayout_3.setContentsMargins(0, 0, 0, 0)
            self.horizontalLayout_3.setSpacing(0)
            self.horizontalLayout_3.setObjectName("horizontalLayout_3")
            self.main_menu_btn = QtWidgets.QPushButton(self.top_left_menu)
            self.main_menu_btn.setMinimumSize(QtCore.QSize(45, 50))
            self.main_menu_btn.setMaximumSize(QtCore.QSize(16777215, 16777215))
            self.main_menu_btn.setStyleSheet("")
            self.main_menu_btn.setText("")
            icon = QtGui.QIcon()
            icon.addPixmap(QtGui.QPixmap(":/icons/Icons/menu2.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
            self.main_menu_btn.setIcon(icon)
            self.main_menu_btn.setIconSize(QtCore.QSize(24, 24))
            self.main_menu_btn.setObjectName("main_menu_btn")
            self.horizontalLayout_3.addWidget(self.main_menu_btn)
            self.hboxlayout.addWidget(self.top_left_menu)
            self.frame = QtWidgets.QFrame(self.header)
            self.frame.setMaximumSize(QtCore.QSize(16777215, 50))
            self.frame.setFrameShape(QtWidgets.QFrame.NoFrame)
            self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
            self.frame.setObjectName("frame")
            self.hboxlayout.addWidget(self.frame)
            self.top_right_btn = QtWidgets.QFrame(self.header)
            self.top_right_btn.setMaximumSize(QtCore.QSize(100, 50))
            self.top_right_btn.setStyleSheet("QPushButton{\n"
    "    border-radius: 5px;\n"
    "}\n"
    "\n"
    "QPushButton:hover {\n"
    "    background-color: rgb(93, 183, 42);\n"
    "}")
            self.top_right_btn.setFrameShape(QtWidgets.QFrame.NoFrame)
            self.top_right_btn.setFrameShadow(QtWidgets.QFrame.Raised)
            self.top_right_btn.setObjectName("top_right_btn")
            self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.top_right_btn)
            self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
            self.horizontalLayout_2.setSpacing(0)
            self.horizontalLayout_2.setObjectName("horizontalLayout_2")
            self.restore_btn = QtWidgets.QPushButton(self.top_right_btn)
            self.restore_btn.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
            self.restore_btn.setText("")
            icon1 = QtGui.QIcon()
            icon1.addPixmap(QtGui.QPixmap(":/icons/Icons/minus.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
            self.restore_btn.setIcon(icon1)
            self.restore_btn.setIconSize(QtCore.QSize(24, 24))
            self.restore_btn.setObjectName("restore_btn")
            self.horizontalLayout_2.addWidget(self.restore_btn)
            self.min_size_btn = QtWidgets.QPushButton(self.top_right_btn)
            self.min_size_btn.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
            self.min_size_btn.setText("")
            icon2 = QtGui.QIcon()
            icon2.addPixmap(QtGui.QPixmap(":/icons/Icons/maximize2.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
            self.min_size_btn.setIcon(icon2)
            self.min_size_btn.setIconSize(QtCore.QSize(24, 24))
            self.min_size_btn.setObjectName("min_size_btn")
            self.horizontalLayout_2.addWidget(self.min_size_btn)
            self.close_btn = QtWidgets.QPushButton(self.top_right_btn)
            self.close_btn.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
    
    # !!!  ------------------------------------------------> v       
            self.close_btn.setStyleSheet("QPushButton {border-radius: 10px;}")
    
            
            self.close_btn.setText("")
            icon3 = QtGui.QIcon()
            icon3.addPixmap(QtGui.QPixmap(":/icons/Icons/x.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
            self.close_btn.setIcon(icon3)
            self.close_btn.setIconSize(QtCore.QSize(24, 24))
            self.close_btn.setObjectName("close_btn")
            self.horizontalLayout_2.addWidget(self.close_btn)
            self.hboxlayout.addWidget(self.top_right_btn)
            self.verticalLayout.addWidget(self.header)
            self.central_part = QtWidgets.QFrame(self.centralwidget)
            self.central_part.setFrameShape(QtWidgets.QFrame.NoFrame)
            self.central_part.setFrameShadow(QtWidgets.QFrame.Raised)
            self.central_part.setObjectName("central_part")
            self.horizontalLayout = QtWidgets.QHBoxLayout(self.central_part)
            self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
            self.horizontalLayout.setSpacing(0)
            self.horizontalLayout.setObjectName("horizontalLayout")
            self.left_menu = QtWidgets.QFrame(self.central_part)
            self.left_menu.setMaximumSize(QtCore.QSize(45, 16777215))
            self.left_menu.setStyleSheet("QFrame#left_menu{\n"
    "    background-color: qlineargradient(spread:pad, x1:0, y1:0.506, x2:1, y2:0.5, stop:0 rgba(52, 146, 35, 255), stop:1 rgba(195, 244, 101, 255));\n"
    "}\n"
    "\n"
    "QPushButton{\n"
    "    padding: 20px 10px;\n"
    "    border: none;\n"
    "    border-radius: 10px;\n"
    "    color: #000000\n"
    "}\n"
    "\n"
    "QPushButton:hover {\n"
    "    background-color: rgb(93, 183, 42);\n"
    "}")
            self.left_menu.setFrameShape(QtWidgets.QFrame.NoFrame)
            self.left_menu.setFrameShadow(QtWidgets.QFrame.Raised)
            self.left_menu.setLineWidth(0)
            self.left_menu.setObjectName("left_menu")
            self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.left_menu)
            self.verticalLayout_2.setContentsMargins(7, 0, 0, 0)
            self.verticalLayout_2.setSpacing(0)
            self.verticalLayout_2.setObjectName("verticalLayout_2")
            self.home_btn = QtWidgets.QPushButton(self.left_menu)
            self.home_btn.setMinimumSize(QtCore.QSize(110, 0))
            self.home_btn.setStyleSheet("background-image: url(:/icons/Icons/home6.png);\n"
    "font: 700 11pt \"Segoe UI\";\n"
    "color: rgb(139, 0, 0);\n"
    "background-repeat: none;\n"
    "padding-left: 45px;\n"
    "background-position: center left;")
            self.home_btn.setObjectName("home_btn")
            self.verticalLayout_2.addWidget(self.home_btn)
            self.bonus_btn = QtWidgets.QPushButton(self.left_menu)
            self.bonus_btn.setMinimumSize(QtCore.QSize(110, 0))
            self.bonus_btn.setStyleSheet("background-image: url(:/icons/Icons/bonuses2.png);\n"
    "font: 700 11pt \"Segoe UI\";\n"
    "color: rgb(139, 0, 0);\n"
    "background-repeat: none;\n"
    "padding-left: 45px;\n"
    "background-position: center left;")
            self.bonus_btn.setIconSize(QtCore.QSize(16, 16))
            self.bonus_btn.setObjectName("bonus_btn")
            self.verticalLayout_2.addWidget(self.bonus_btn)
            self.invoice_btn = QtWidgets.QPushButton(self.left_menu)
            self.invoice_btn.setMinimumSize(QtCore.QSize(110, 0))
            self.invoice_btn.setStyleSheet("background-image: url(:/icons/Icons/invoices.png);\n"
    "font: 700 11pt \"Segoe UI\";\n"
    "color: rgb(139, 0, 0);\n"
    "background-repeat: none;\n"
    "padding-left: 43px;\n"
    "background-position: center left;")
            self.invoice_btn.setObjectName("invoice_btn")
            self.verticalLayout_2.addWidget(self.invoice_btn)
            self.client_btn = QtWidgets.QPushButton(self.left_menu)
            self.client_btn.setMinimumSize(QtCore.QSize(110, 0))
            self.client_btn.setStyleSheet("background-image: url(:/icons/Icons/customers.png);\n"
    "font: 700 11pt \"Segoe UI\";\n"
    "color: rgb(139, 0, 0);\n"
    "background-repeat: none;\n"
    "padding-left: 45px;\n"
    "background-position: center left;")
            self.client_btn.setObjectName("client_btn")
            self.verticalLayout_2.addWidget(self.client_btn)
            self.product_btn = QtWidgets.QPushButton(self.left_menu)
            self.product_btn.setMinimumSize(QtCore.QSize(110, 0))
            self.product_btn.setStyleSheet("background-image: url(:/icons/Icons/products2.png);\n"
    "font: 700 11pt \"Segoe UI\";\n"
    "color: rgb(139, 0, 0);\n"
    "background-repeat: none;\n"
    "padding-left: 45px;\n"
    "background-position: center left;")
            self.product_btn.setObjectName("product_btn")
            self.verticalLayout_2.addWidget(self.product_btn)
            self.horizontalLayout.addWidget(self.left_menu)
            self.main_part = QtWidgets.QFrame(self.central_part)
            self.main_part.setAutoFillBackground(False)
            self.main_part.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.194037, y1:0.821, x2:0.534, y2:0.422619, stop:0.0105263 rgba(44, 146, 25, 255), stop:1 rgba(195, 244, 101, 255));\n"
    "background-position: center;\n"
    "background-repeat: no-repeat;\n")
    
    # ???"background-size: 100%")
    
            self.main_part.setFrameShape(QtWidgets.QFrame.NoFrame)
            self.main_part.setFrameShadow(QtWidgets.QFrame.Raised)
            self.main_part.setLineWidth(0)
            self.main_part.setObjectName("main_part")
            self.horizontalLayout.addWidget(self.main_part)
            self.verticalLayout.addWidget(self.central_part)
            self.futter_menu = QtWidgets.QFrame(self.centralwidget)
            self.futter_menu.setMaximumSize(QtCore.QSize(16777215, 50))
            self.futter_menu.setStyleSheet("QFrame#futter_menu{\n"
    "    background-color: qlineargradient(spread:pad, x1:0.1, y1:0.215909, x2:0.959, y2:0.909091, stop:0 rgba(52, 146, 35, 255), stop:1 rgba(195, 244, 101, 255));\n"
    "}\n"
    "\n"
    "")
            self.futter_menu.setFrameShape(QtWidgets.QFrame.NoFrame)
            self.futter_menu.setFrameShadow(QtWidgets.QFrame.Raised)
            self.futter_menu.setLineWidth(0)
            self.futter_menu.setObjectName("futter_menu")
            self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.futter_menu)
            self.horizontalLayout_4.setContentsMargins(0, 0, 0, 0)
            self.horizontalLayout_4.setSpacing(0)
            self.horizontalLayout_4.setObjectName("horizontalLayout_4")
            self.frame_2 = QtWidgets.QFrame(self.futter_menu)
            self.frame_2.setMaximumSize(QtCore.QSize(16777215, 50))
            self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
            self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
            self.frame_2.setObjectName("frame_2")
            self.horizontalLayout_4.addWidget(self.frame_2)
            self.frame_3 = QtWidgets.QFrame(self.futter_menu)
            self.frame_3.setMaximumSize(QtCore.QSize(150, 50))
            self.frame_3.setFrameShape(QtWidgets.QFrame.StyledPanel)
            self.frame_3.setFrameShadow(QtWidgets.QFrame.Raised)
            self.frame_3.setObjectName("frame_3")
            self.horizontalLayout_5 = QtWidgets.QHBoxLayout(self.frame_3)
            self.horizontalLayout_5.setObjectName("horizontalLayout_5")
            self.back_btn = QtWidgets.QPushButton(self.frame_3)
            self.back_btn.setMinimumSize(QtCore.QSize(0, 27))
            self.back_btn.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
            self.back_btn.setStyleSheet("QPushButton{\n"
    "    border-radius: 5px;\n"
    "    font: 700 10pt \"Segoe UI\";\n"
    "    color: rgb(139, 0, 0);\n"
    "    background-color: qlineargradient(spread:pad, x1:0.1, y1:0.215909, x2:0.959, y2:0.909091, stop:0 rgba(52, 146, 35, 255), stop:1 rgba(195, 244, 101, 255));\n"
    "}\n"
    "\n"
    "QPushButton:hover {\n"
    "    background-color: rgb(93, 183, 42);\n"
    "}")
            self.back_btn.setObjectName("back_btn")
            self.horizontalLayout_5.addWidget(self.back_btn)
            self.horizontalLayout_4.addWidget(self.frame_3)
            self.verticalLayout.addWidget(self.futter_menu)
            MainWindow.setCentralWidget(self.centralwidget)
    
            self.retranslateUi(MainWindow)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
            self.home_btn.setText(_translate("MainWindow", "HOME"))
            self.bonus_btn.setText(_translate("MainWindow", "Bonus"))
            self.invoice_btn.setText(_translate("MainWindow", "Invoice"))
            self.client_btn.setText(_translate("MainWindow", "Clients"))
            self.product_btn.setText(_translate("MainWindow", "Product"))
            self.back_btn.setText(_translate("MainWindow", "Back"))
    
    # !!!  vvvvvvvv <-------------------------------------------------------------   !!!
    import screens.rc_resource
    

    在此处输入图像描述

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5