从另一个文件打开 GUI 时键入错误

Type Error when opening GUI from another file

尝试在另一个文件中打开 pyqt5 GUI 时出现错误。

main.py

from main_scrn import Ui_MainWindow
from sload_page import Ui_SLoadWindow
from PySide2.QtWidgets import *

class SLoadWindow(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.ui = Ui_SLoadWindow()
        self.ui.setupUi(self)

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    window = SLoadWindow()
    sys.exit(app.exec_())

sload_page.py

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_SLoadWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1920, 1080)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(0, 0, 1920, 1080))
        self.label.setObjectName("label")
        self.load1Button = QtWidgets.QPushButton(self.centralwidget)
        self.load1Button.setGeometry(QtCore.QRect(880, 520, 161, 71))
        self.load1Button.setStyleSheet("QPushButton {\n"
"    border: 2px solid #fff;\n"
"    color: #fff;\n"
"    font-size: 20px;\n"
"    border-radius: 5px;\n"
"    background: rgba(132, 132, 132, 180)\n"
"}\n"
"\n"
"QPushButton:hover {\n"
"    border: 2px solid rgb(182, 182, 182);\n"
"    color: rgb(182, 182, 182);\n"
"    font-size: 20px;\n"
"    border-radius: 5px;\n"
"}")
        self.load1Button.setObjectName("load1Button")
        self.load2Button = QtWidgets.QPushButton(self.centralwidget)
        self.load2Button.setGeometry(QtCore.QRect(880, 610, 161, 71))
        self.load2Button.setStyleSheet("QPushButton {\n"
"    border: 2px solid #fff;\n"
"    color: #fff;\n"
"    font-size: 20px;\n"
"    border-radius: 5px;\n"
"    background: rgba(132, 132, 132, 180)\n"
"}\n"
"\n"
"QPushButton:hover {\n"
"    border: 2px solid rgb(182, 182, 182);\n"
"    color: rgb(182, 182, 182);\n"
"    font-size: 20px;\n"
"    border-radius: 5px;\n"
"}")
        self.load2Button.setObjectName("load2Button")
        self.load3Button = QtWidgets.QPushButton(self.centralwidget)
        self.load3Button.setGeometry(QtCore.QRect(880, 700, 161, 71))
        self.load3Button.setStyleSheet("QPushButton {\n"
"    border: 2px solid #fff;\n"
"    color: #fff;\n"
"    font-size: 20px;\n"
"    border-radius: 5px;\n"
"    background: rgba(132, 132, 132, 180)\n"
"}\n"
"\n"
"QPushButton:hover {\n"
"    border: 2px solid rgb(182, 182, 182);\n"
"    color: rgb(182, 182, 182);\n"
"    font-size: 20px;\n"
"    border-radius: 5px;\n"
"}")
        self.load3Button.setObjectName("load3Button")
        self.load4Button = QtWidgets.QPushButton(self.centralwidget)
        self.load4Button.setGeometry(QtCore.QRect(880, 790, 161, 71))
        self.load4Button.setStyleSheet("QPushButton {\n"
"    border: 2px solid #fff;\n"
"    color: #fff;\n"
"    font-size: 20px;\n"
"    border-radius: 5px;\n"
"    background: rgba(132, 132, 132, 180)\n"
"}\n"
"\n"
"QPushButton:hover {\n"
"    border: 2px solid rgb(182, 182, 182);\n"
"    color: rgb(182, 182, 182);\n"
"    font-size: 20px;\n"
"    border-radius: 5px;\n"
"}")
        self.load4Button.setObjectName("load4Button")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "<html><head/><body><p><img src=\":/main_image/main.png\"/></p></body></html>"))
        self.load1Button.setText(_translate("MainWindow", "Load 1"))
        self.load2Button.setText(_translate("MainWindow", "Load 2"))
        self.load3Button.setText(_translate("MainWindow", "Load 3"))
        self.load4Button.setText(_translate("MainWindow", "Load 4"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_SLoadWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

我对 PyQt5 知之甚少,因为我对它还很陌生,而且厌倦了 tkinter,所以如果你能告诉我任何事情或提出改进建议,那将非常有帮助,我将不胜感激。

问题是您错误地组合了库。尽管 PyQt5 和 PySide2 是 Qt 的绑定,但它们是不同的实现,因此它们不兼容,因此解决方案:

  • 或使用PyQt5:在main.py

    中改为from PyQt5.QtWidgets import *
  • 或使用 PySide2 但不能同时使用:在 sload_page.py

    中更改为 from PySide2 import QtCore, QtGui, QtWidgets

但不要同时使用这两个库。