首页 > 解决方案 > 如何通过单击按钮加载生成的 PyQt5 UI 代码文件?

问题描述

我在 python 代码中从 Qt Designer 生成了 2 个 UI 文件。在第一个 UI 中有按钮,我想通过单击一个按钮转换到另一个 UI。问题是,当我创建一个函数来执行此操作时,它会显示此错误:“AttributeError: type object 'Ui_Dialog' has no attribute 'setObjectName'” 我该如何正确执行此操作?

第一个用户界面代码:

import sys
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui, QtWidgets
from RegisterCode import Ui_Dialog2


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(408, 719)
        self.groupBox = QtWidgets.QGroupBox(Dialog)
        self.groupBox.setGeometry(QtCore.QRect(0, 0, 421, 721))
        self.groupBox.setStyleSheet("background-color: rgb(188, 218, 255);")
        self.groupBox.setObjectName("groupBox")
        self.layoutWidget = QtWidgets.QWidget(self.groupBox)
        self.layoutWidget.setGeometry(QtCore.QRect(50, 200, 311, 261))
        self.layoutWidget.setObjectName("layoutWidget")
        self.verticalLayout_5 = QtWidgets.QVBoxLayout(self.layoutWidget)
        self.verticalLayout_5.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_5.setObjectName("verticalLayout_5")
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.labelUsername = QtWidgets.QLabel(self.layoutWidget)
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.labelUsername.setFont(font)
        self.labelUsername.setStyleSheet("color: rgb(86, 98, 122);")
        self.labelUsername.setObjectName("labelUsername")
        self.verticalLayout.addWidget(self.labelUsername)
        self.verticalLayout_5.addLayout(self.verticalLayout)
        self.username = QtWidgets.QLineEdit(self.layoutWidget)
        self.username.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.username.setObjectName("username")
        self.verticalLayout_5.addWidget(self.username)
        self.labelPassword = QtWidgets.QLabel(self.layoutWidget)
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.labelPassword.setFont(font)
        self.labelPassword.setStyleSheet("color: rgb(86, 98, 122);")
        self.labelPassword.setObjectName("labelPassword")
        self.verticalLayout_5.addWidget(self.labelPassword)
        self.password = QtWidgets.QLineEdit(self.layoutWidget)
        self.password.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.password.setObjectName("password")
        self.verticalLayout_5.addWidget(self.password)
        self.verticalLayout_2 = QtWidgets.QVBoxLayout()
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.loginbutton = QtWidgets.QPushButton(self.layoutWidget)
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.loginbutton.setFont(font)
        self.loginbutton.setStyleSheet("color: rgb(86, 98, 122);\n"
"background-color: rgb(227, 180, 180);")
        self.loginbutton.setObjectName("loginbutton")
        self.verticalLayout_2.addWidget(self.loginbutton)
        self.verticalLayout_5.addLayout(self.verticalLayout_2)
        self.BOOKALL_2 = QtWidgets.QLabel(self.groupBox)
        self.BOOKALL_2.setGeometry(QtCore.QRect(150, 70, 171, 51))
        font = QtGui.QFont()
        font.setFamily("Forte")
        font.setPointSize(20)
        font.setBold(False)
        font.setWeight(50)
        self.BOOKALL_2.setFont(font)
        self.BOOKALL_2.setStyleSheet("background-color: rgb(188, 218, 255);\n"
"color: rgb(27, 36, 156);\n"
"\n"
"")
        self.BOOKALL_2.setObjectName("BOOKALL_2")
        self.frame = QtWidgets.QFrame(self.groupBox)
        self.frame.setGeometry(QtCore.QRect(40, 190, 331, 281))
        self.frame.setStyleSheet("background-color: rgb(205, 255, 228);")
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.label = QtWidgets.QLabel(self.groupBox)
        self.label.setGeometry(QtCore.QRect(50, 480, 161, 31))
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setStyleSheet("color: rgb(45, 64, 88);")
        self.label.setObjectName("label")
        self.createaccButton = QtWidgets.QPushButton(self.groupBox)
        self.createaccButton.setGeometry(QtCore.QRect(240, 490, 121, 41))
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.createaccButton.setFont(font)
        self.createaccButton.setStyleSheet("color: rgb(136, 0, 0);\n"
"background-color: rgb(227, 180, 180);")
        self.createaccButton.setObjectName("createaccButton")
        self.frame.raise_()
        self.layoutWidget.raise_()
        self.BOOKALL_2.raise_()
        self.label.raise_()
        self.createaccButton.raise_()
        
        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)
        self.createaccButton.clicked.connect(self.gotocreate)
        
    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        #Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.groupBox.setTitle(_translate("Dialog", "Sign in"))
        self.labelUsername.setText(_translate("Dialog", "Username"))
        self.labelPassword.setText(_translate("Dialog", "Password"))
        self.loginbutton.setText(_translate("Dialog", "Sign In"))
        self.BOOKALL_2.setText(_translate("Dialog", "BOOKALL"))
        self.label.setText(_translate("Dialog", "Don\'t have an account?"))
        self.createaccButton.setText(_translate("Dialog", "Create Account"))

    def gotocreate(self):
        createacc = Ui_Dialog2()
        createacc.setupUi(Ui_Dialog)
        createacc.show()

第二个用户界面代码:

import sys
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog2(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Ui_Dialog2")
        Dialog.resize(403, 719)
        self.groupBox = QtWidgets.QGroupBox(Dialog)
        self.groupBox.setGeometry(QtCore.QRect(-10, 0, 421, 721))
        self.groupBox.setStyleSheet("background-color: rgb(188, 218, 255);")
        self.groupBox.setObjectName("groupBox")
        self.BOOKALL_2 = QtWidgets.QLabel(self.groupBox)
        self.BOOKALL_2.setGeometry(QtCore.QRect(150, 40, 171, 51))
        font = QtGui.QFont()
        font.setFamily("Forte")
        font.setPointSize(20)
        font.setBold(False)
        font.setWeight(50)
        self.BOOKALL_2.setFont(font)
        self.BOOKALL_2.setStyleSheet("background-color: rgb(188, 218, 255);\n"
"color: rgb(27, 36, 156);\n"
"\n"
"")
        self.BOOKALL_2.setObjectName("BOOKALL_2")
        self.frame = QtWidgets.QFrame(self.groupBox)
        self.frame.setGeometry(QtCore.QRect(50, 100, 331, 461))
        self.frame.setStyleSheet("background-color: rgb(205, 255, 228);")
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.layoutWidget = QtWidgets.QWidget(self.frame)
        self.layoutWidget.setGeometry(QtCore.QRect(10, 10, 311, 201))
        self.layoutWidget.setObjectName("layoutWidget")
        self.verticalLayout_5 = QtWidgets.QVBoxLayout(self.layoutWidget)
        self.verticalLayout_5.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_5.setObjectName("verticalLayout_5")
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.labelUsername = QtWidgets.QLabel(self.layoutWidget)
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.labelUsername.setFont(font)
        self.labelUsername.setStyleSheet("color: rgb(86, 98, 122);\n"
"background-color: rgb(188, 218, 255);")
        self.labelUsername.setObjectName("labelUsername")
        self.verticalLayout.addWidget(self.labelUsername)
        self.verticalLayout_5.addLayout(self.verticalLayout)
        self.username = QtWidgets.QLineEdit(self.layoutWidget)
        self.username.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.username.setObjectName("username")
        self.verticalLayout_5.addWidget(self.username)
        self.label_password = QtWidgets.QLabel(self.layoutWidget)
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_password.setFont(font)
        self.label_password.setStyleSheet("color: rgb(86, 98, 122);\n"
"background-color: rgb(188, 218, 255);")
        self.label_password.setObjectName("label_password")
        self.verticalLayout_5.addWidget(self.label_password)
        self.password = QtWidgets.QLineEdit(self.layoutWidget)
        self.password.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.password.setObjectName("password")
        self.verticalLayout_5.addWidget(self.password)
        self.layoutWidget1 = QtWidgets.QWidget(self.frame)
        self.layoutWidget1.setGeometry(QtCore.QRect(10, 220, 311, 191))
        self.layoutWidget1.setObjectName("layoutWidget1")
        self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.layoutWidget1)
        self.verticalLayout_4.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_4.setObjectName("verticalLayout_4")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout()
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.label_repeatpassword = QtWidgets.QLabel(self.layoutWidget1)
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_repeatpassword.setFont(font)
        self.label_repeatpassword.setStyleSheet("color: rgb(86, 98, 122);\n"
"background-color: rgb(188, 218, 255);")
        self.label_repeatpassword.setObjectName("label_repeatpassword")
        self.verticalLayout_2.addWidget(self.label_repeatpassword)
        self.repeatpassword = QtWidgets.QLineEdit(self.layoutWidget1)
        self.repeatpassword.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.repeatpassword.setObjectName("repeatpassword")
        self.verticalLayout_2.addWidget(self.repeatpassword)
        self.label_email = QtWidgets.QLabel(self.layoutWidget1)
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_email.setFont(font)
        self.label_email.setStyleSheet("color: rgb(86, 98, 122);\n"
"background-color: rgb(188, 218, 255);")
        self.label_email.setObjectName("label_email")
        self.verticalLayout_2.addWidget(self.label_email)
        self.email = QtWidgets.QLineEdit(self.layoutWidget1)
        self.email.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.email.setObjectName("email")
        self.verticalLayout_2.addWidget(self.email)
        self.verticalLayout_4.addLayout(self.verticalLayout_2)
        self.checkBox = QtWidgets.QCheckBox(self.frame)
        self.checkBox.setGeometry(QtCore.QRect(10, 420, 141, 31))
        font = QtGui.QFont()
        font.setPointSize(11)
        font.setBold(True)
        font.setWeight(75)
        self.checkBox.setFont(font)
        self.checkBox.setStyleSheet("color: rgb(86, 98, 122);\n"
"background-color: rgb(188, 218, 255);")
        self.checkBox.setObjectName("checkBox")
        self.checkBox2 = QtWidgets.QCheckBox(self.frame)
        self.checkBox2.setGeometry(QtCore.QRect(170, 420, 151, 31))
        font = QtGui.QFont()
        font.setPointSize(11)
        font.setBold(True)
        font.setWeight(75)
        self.checkBox2.setFont(font)
        self.checkBox2.setStyleSheet("color: rgb(86, 98, 122);\n"
"background-color: rgb(188, 218, 255);")
        self.checkBox2.setObjectName("checkBox2")
        self.signupbutton = QtWidgets.QPushButton(self.groupBox)
        self.signupbutton.setGeometry(QtCore.QRect(70, 650, 307, 27))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.signupbutton.setFont(font)
        self.signupbutton.setStyleSheet("color: rgb(86, 98, 122);\n"
"background-color: rgb(227, 180, 180);")
        self.signupbutton.setObjectName("signupbutton")
        self.diffpasswords = QtWidgets.QLabel(self.groupBox)
        self.diffpasswords.setGeometry(QtCore.QRect(140, 570, 231, 41))
        font = QtGui.QFont()
        font.setPointSize(11)
        font.setBold(True)
        font.setWeight(75)
        self.diffpasswords.setFont(font)
        self.diffpasswords.setStyleSheet("color: rgb(59, 66, 108);")
        self.diffpasswords.setObjectName("diffpasswords")
        self.gotologinButton = QtWidgets.QPushButton(self.groupBox)
        self.gotologinButton.setGeometry(QtCore.QRect(270, 570, 111, 41))
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.gotologinButton.setFont(font)
        self.gotologinButton.setStyleSheet("color: rgb(136, 0, 0);\n"
"background-color: rgb(227, 180, 180);")
        self.gotologinButton.setObjectName("gotologinButton")
        self.frame.raise_()
        self.BOOKALL_2.raise_()
        self.signupbutton.raise_()
        self.diffpasswords.raise_()
        self.gotologinButton.raise_()

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

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.groupBox.setTitle(_translate("Dialog", "Signup"))
        self.BOOKALL_2.setText(_translate("Dialog", "BOOKALL"))
        self.labelUsername.setText(_translate("Dialog", "Username"))
        self.label_password.setText(_translate("Dialog", "Password"))
        self.label_repeatpassword.setText(_translate("Dialog", "Repeat Password"))
        self.label_email.setText(_translate("Dialog", "Email"))
        self.checkBox.setText(_translate("Dialog", "Simple User"))
        self.checkBox2.setText(_translate("Dialog", "Businessman"))
        self.signupbutton.setText(_translate("Dialog", "Sign up"))
        self.diffpasswords.setText(_translate("Dialog", "Already a user?"))
        self.gotologinButton.setText(_translate("Dialog", "Login"))

主窗口文件:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
from Login import Ui_Dialog 
from RegisterCode import Ui_Dialog2

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()


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

在 MainWindow 文件中,我创建了一个 MainWindow 类的实例,它是一个 QMainWindow,在该实例中,我从第一个文件中设置了 Ui_Dialog 类。这工作正常。在 Ui_Dialog(第一个 UI)中有一个名为 createaccButton 的按钮,它调用 gotocreate 函数以转换到(第二个 UI)Ui_Dialog2。单击按钮时,我收到此错误:“AttributeError:类型对象'Ui_Dialog'没有属性'setObjectName'”。有没有更好的方法来做这个开关?还有什么是方法中的对话框:“def setupUi(self,Dialog)”以及我需要将类类型(class Ui_Dialog(object))转换为什么才能继承属性?

标签: pythonuser-interfacepyqtpyqt5qt-designer

解决方案


推荐阅读