9

我用 PyQt GUI 做了一个简单的程序,并编译了 '.ui' 文件。

# Form implementation generated from reading ui file 'main.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.setEnabled(True)
        MainWindow.resize(441, 255)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
        MainWindow.setSizePolicy(sizePolicy)
        MainWindow.setAutoFillBackground(False)
        MainWindow.setTabShape(QtWidgets.QTabWidget.Rounded)
        self.frame = QtWidgets.QWidget(MainWindow)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.frame.sizePolicy().hasHeightForWidth())
        self.frame.setSizePolicy(sizePolicy)
        self.frame.setObjectName("frame")
        self.tab = QtWidgets.QTabWidget(self.frame)
        self.tab.setGeometry(QtCore.QRect(10, 10, 421, 201))
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.tab.sizePolicy().hasHeightForWidth())
        self.tab.setSizePolicy(sizePolicy)
        font = QtGui.QFont()
        font.setFamily("UltraLight")
        font.setPointSize(10)
        self.tab.setFont(font)
        self.tab.setTabShape(QtWidgets.QTabWidget.Triangular)
        self.tab.setMovable(False)
        self.tab.setTabBarAutoHide(False)
        self.tab.setObjectName("tab")
        self.tabOption = QtWidgets.QWidget()
        self.tabOption.setObjectName("tabOption")
        self.profileList = QtWidgets.QComboBox(self.tabOption)
        self.profileList.setGeometry(QtCore.QRect(60, 10, 121, 20))
        font = QtGui.QFont()
        font.setFamily("UltraLight")
        font.setPointSize(10)
        self.profileList.setFont(font)
        self.profileList.setObjectName("profileList")
        self.ramInput = QtWidgets.QLineEdit(self.tabOption)
        self.ramInput.setGeometry(QtCore.QRect(50, 40, 81, 20))
        self.ramInput.setObjectName("ramInput")
        self.ramLabel = QtWidgets.QLabel(self.tabOption)
        self.ramLabel.setGeometry(QtCore.QRect(10, 40, 31, 21))
        font = QtGui.QFont()
        font.setFamily("나눔바른고딕 UltraLight")
        font.setPointSize(10)
        self.ramLabel.setFont(font)
        self.ramLabel.setObjectName("ramLabel")
        self.ramUnit = QtWidgets.QComboBox(self.tabOption)
        self.ramUnit.setGeometry(QtCore.QRect(140, 40, 41, 22))
        self.ramUnit.setObjectName("ramUnit")
        self.ramUnit.addItem("")
        self.ramUnit.addItem("")
        self.ramUnit.addItem("")
        self.profileLabel = QtWidgets.QLabel(self.tabOption)
        self.profileLabel.setGeometry(QtCore.QRect(10, 10, 41, 21))
        font = QtGui.QFont()
        font.setFamily("UltraLight")
        font.setPointSize(10)
        self.profileLabel.setFont(font)
        self.profileLabel.setObjectName("profileLabel")
        self.logo = QtWidgets.QLabel(self.tabOption)
        self.logo.setGeometry(QtCore.QRect(180, 10, 231, 151))
        self.logo.setStyleSheet("image: url(:/logo/logo.png);")
        self.logo.setText("")
        self.logo.setObjectName("logo")
        self.javaLabel = QtWidgets.QLabel(self.tabOption)
        self.javaLabel.setGeometry(QtCore.QRect(10, 70, 61, 21))
        font = QtGui.QFont()
        font.setFamily("UltraLight")
        font.setPointSize(10)
        self.javaLabel.setFont(font)
        self.javaLabel.setObjectName("javaLabel")
        self.javaInput = QtWidgets.QLineEdit(self.tabOption)
        self.javaInput.setGeometry(QtCore.QRect(80, 70, 101, 20))
        self.javaInput.setObjectName("javaInput")
        self.javaLabelOptinal = QtWidgets.QLabel(self.tabOption)
        self.javaLabelOptinal.setGeometry(QtCore.QRect(10, 80, 61, 21))
        font = QtGui.QFont()
        font.setFamily("나눔바른고딕 UltraLight")
        font.setPointSize(6)
        self.javaLabelOptinal.setFont(font)
        self.javaLabelOptinal.setObjectName("javaLabelOptinal")
        self.launch = QtWidgets.QCommandLinkButton(self.tabOption)
        self.launch.setGeometry(QtCore.QRect(10, 110, 171, 41))
        self.launch.setObjectName("launch")
        self.tab.addTab(self.tabOption, "")
        self.tabConsole = QtWidgets.QWidget()
        self.tabConsole.setObjectName("tabConsole")
        self.console = QtWidgets.QTextBrowser(self.tabConsole)
        self.console.setGeometry(QtCore.QRect(10, 10, 391, 131))
        self.console.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.console.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
        self.console.setPlaceholderText("")
        self.console.setObjectName("console")
        self.commandInput = QtWidgets.QLineEdit(self.tabConsole)
        self.commandInput.setGeometry(QtCore.QRect(10, 150, 391, 20))
        self.commandInput.setText("")
        self.commandInput.setObjectName("commandInput")
        self.tab.addTab(self.tabConsole, "")
        MainWindow.setCentralWidget(self.frame)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setEnabled(True)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 441, 21))
        font = QtGui.QFont()
        font.setFamily("나눔바른고딕 UltraLight")
        font.setPointSize(10)
        self.menubar.setFont(font)
        self.menubar.setToolTip("Settings about profiles")
        self.menubar.setObjectName("menubar")
        self.menuProfiles = QtWidgets.QMenu(self.menubar)
        self.menuProfiles.setObjectName("menuProfiles")
        self.menuSettings = QtWidgets.QMenu(self.menubar)
        self.menuSettings.setObjectName("menuSettings")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        font = QtGui.QFont()
        font.setFamily("")
        font.setPointSize(10)
        self.statusbar.setFont(font)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.actionCreate_new_profile = QtWidgets.QAction(MainWindow)
        font = QtGui.QFont()
        font.setFamily("나눔바른고딕 UltraLight")
        font.setPointSize(10)
        self.actionCreate_new_profile.setFont(font)
        self.actionCreate_new_profile.setObjectName("actionCreate_new_profile")
        self.actionDelete_a_profile = QtWidgets.QAction(MainWindow)
        font = QtGui.QFont()
        font.setFamily("나눔바른고딕 UltraLight")
        font.setPointSize(10)
        self.actionDelete_a_profile.setFont(font)
        self.actionDelete_a_profile.setObjectName("actionDelete_a_profile")
        self.actionProfile_Settings = QtWidgets.QAction(MainWindow)
        font = QtGui.QFont()
        font.setFamily("UltraLight")
        font.setPointSize(10)
        self.actionProfile_Settings.setFont(font)
        self.actionProfile_Settings.setObjectName("actionProfile_Settings")
        self.actionGlobal_settings = QtWidgets.QAction(MainWindow)
        font = QtGui.QFont()
        font.setFamily("UltraLight")
        font.setPointSize(10)
        self.actionGlobal_settings.setFont(font)
        self.actionGlobal_settings.setObjectName("actionGlobal_settings")
        self.actionAuto_completes = QtWidgets.QAction(MainWindow)
        font = QtGui.QFont()
        font.setFamily("UltraLight")
        font.setPointSize(10)
        self.actionAuto_completes.setFont(font)
        self.actionAuto_completes.setObjectName("actionAuto_completes")
        self.menuProfiles.addAction(self.actionCreate_new_profile)
        self.menuProfiles.addAction(self.actionDelete_a_profile)
        self.menuSettings.addAction(self.actionProfile_Settings)
        self.menuSettings.addAction(self.actionGlobal_settings)
        self.menuSettings.addSeparator()
        self.menuSettings.addAction(self.actionAuto_completes)
        self.menubar.addAction(self.menuProfiles.menuAction())
        self.menubar.addAction(self.menuSettings.menuAction())

        self.retranslateUi(MainWindow)
        self.tab.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.ramLabel.setText(_translate("MainWindow", "RAM:"))
        self.ramUnit.setItemText(0, _translate("MainWindow", "GB"))
        self.ramUnit.setItemText(1, _translate("MainWindow", "MB"))
        self.ramUnit.setItemText(2, _translate("MainWindow", "KB"))
        self.profileLabel.setText(_translate("MainWindow", "Profile:"))
        self.javaLabel.setText(_translate("MainWindow", "Java Path:"))
        self.javaLabelOptinal.setText(_translate("MainWindow", "(Optinal)"))
        self.launch.setText(_translate("MainWindow", "Launch Bukkit!"))
        self.tab.setTabText(self.tab.indexOf(self.tabOption), _translate("MainWindow", "Launch Options"))
        self.tab.setTabText(self.tab.indexOf(self.tabConsole), _translate("MainWindow", "Console"))
        self.menuProfiles.setTitle(_translate("MainWindow", "Profiles"))
        self.menuSettings.setTitle(_translate("MainWindow", "Settings"))
        self.actionCreate_new_profile.setText(_translate("MainWindow", "Create a new profile.."))
        self.actionCreate_new_profile.setShortcut(_translate("MainWindow", "Alt+C"))
        self.actionDelete_a_profile.setText(_translate("MainWindow", "Delete a profile.."))
        self.actionDelete_a_profile.setShortcut(_translate("MainWindow", "Alt+D"))
        self.actionProfile_Settings.setText(_translate("MainWindow", "Profile settings.."))
        self.actionProfile_Settings.setShortcut(_translate("MainWindow", "Alt+S"))
        self.actionGlobal_settings.setText(_translate("MainWindow", "Global settings.."))
        self.actionGlobal_settings.setShortcut(_translate("MainWindow", "Alt+Shift+S"))
        self.actionAuto_completes.setText(_translate("MainWindow", "Auto completes.."))
        self.actionAuto_completes.setShortcut(_translate("MainWindow", "Alt+A"))

import resource_rc

但它会导致如下错误。

ImportError:没有名为“resource_rc”的模块

我已经尝试在 StackOverflow 中找到此错误,但没有人收到此错误。

我正在使用 Python 3.5 和 PyQt 5.6。(我知道 PyQt 与 Python 3.5 不兼容,但除此之外我没有任何错误)


现在,在我删除最后一行代码后,代码可以正常工作:'import resource_rc'

但这条线到底在做什么?

4

5 回答 5

24

您应该有一个名为 的文件resource.qrc,必须将其转换为.py,或者您可以通过执行以下操作来完成:

pyrcc5 resource.qrc -o resource_rc.py
于 2016-12-23T19:13:10.303 回答
2

当你编译一个 .ui 文件时,它使用 resources_rc.py(它也必须被编译)来存储它的资源。例如,如果您打开 qrc 文件,您将看到它存储的项目。如果您的 ui 使用了 resources_rc.py, 文件中的任何内容,您将收到您看到的错误。

于 2016-12-23T12:20:04.030 回答
1

导入资源_rc

您可以使用 --from-imports 选项解决此问题。

pyuic4 -h

--from-imports      generate imports relative to '.'

例子:

pyuic4 --from-imports -w -o aboutBA.py aboutBA.ui
于 2020-12-26T18:05:56.773 回答
1

使用python3,我使用这个:

pyrcc5 file.qrc -o file.py

https://i.stack.imgur.com/UhvOS.png

于 2021-01-02T17:48:09.733 回答
0

确保使用 PyQT5 选择您的环境!

conda activate your_env_name

试试上面的命令。确保您已安装 Anaconda。

如果您没有安装 Pyqt5,请使用以下命令进行安装。

conda install -c anaconda pyqt
pip install PyQt5
于 2021-12-06T12:16:36.903 回答