目录
一、登录界面长下面这样,由qt designer设计 login.py
二、登录界面 逻辑代码loginLogic.py
三、主界面comsumeType.py
四、主界面逻辑代码comsumeTypeLogic.py
五、登录界面跳转至主界面
六、主界面回到登录界面
一、登录界面长下面这样,由qt designer设计 login.py
# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'login.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.from PyQt5 import QtCore, QtGui, QtWidgetsclass Ui_MainWindow(object):def setupUi(self, MainWindow):MainWindow.setObjectName("MainWindow")MainWindow.resize(416, 271)MainWindow.setMinimumSize(QtCore.QSize(416, 271))MainWindow.setMaximumSize(QtCore.QSize(416, 271))self.centralwidget = QtWidgets.QWidget(MainWindow)self.centralwidget.setObjectName("centralwidget")self.label = QtWidgets.QLabel(self.centralwidget)self.label.setGeometry(QtCore.QRect(70, 40, 291, 31))font = QtGui.QFont()font.setPointSize(17)font.setBold(True)font.setWeight(75)self.label.setFont(font)self.label.setAlignment(QtCore.Qt.AlignCenter)self.label.setObjectName("label")self.label_2 = QtWidgets.QLabel(self.centralwidget)self.label_2.setGeometry(QtCore.QRect(80, 110, 101, 21))font = QtGui.QFont()font.setPointSize(11)self.label_2.setFont(font)self.label_2.setObjectName("label_2")self.label_3 = QtWidgets.QLabel(self.centralwidget)self.label_3.setGeometry(QtCore.QRect(80, 150, 61, 21))font = QtGui.QFont()font.setPointSize(11)self.label_3.setFont(font)self.label_3.setObjectName("label_3")self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)self.lineEdit.setGeometry(QtCore.QRect(160, 110, 171, 20))self.lineEdit.setObjectName("lineEdit")self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)self.lineEdit_2.setGeometry(QtCore.QRect(160, 150, 171, 20))self.lineEdit_2.setObjectName("lineEdit_2")self.pushButton = QtWidgets.QPushButton(self.centralwidget)self.pushButton.setGeometry(QtCore.QRect(100, 200, 81, 31))font = QtGui.QFont()font.setPointSize(11)self.pushButton.setFont(font)self.pushButton.setObjectName("pushButton")self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)self.pushButton_2.setGeometry(QtCore.QRect(240, 200, 81, 31))font = QtGui.QFont()font.setPointSize(11)self.pushButton_2.setFont(font)self.pushButton_2.setObjectName("pushButton_2")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.translateMainWindow.setWindowTitle(_translate("MainWindow", "欢迎"))self.label.setText(_translate("MainWindow", "管理系统"))self.label_2.setText(_translate("MainWindow", "用户名:"))self.label_3.setText(_translate("MainWindow", "密 码:"))self.pushButton.setText(_translate("MainWindow", "登录"))self.pushButton_2.setText(_translate("MainWindow", "注册"))if __name__ == "__main__":import sysapp = QtWidgets.QApplication(sys.argv)MainWindow = QtWidgets.QMainWindow()ui = Ui_MainWindow()ui.setupUi(MainWindow)MainWindow.show()sys.exit(app.exec_())
二、登录界面 逻辑代码loginLogic.py
from login import Ui_MainWindow as loginUI
from comsumeTypeLogic import comsumeTypeLogic as CSTLogicfrom PyQt5.QtWidgets import QMessageBox,QMainWindow,QApplication
from PyQt5.QtGui import QIcon
from PyQt5 import QtCoreimport sysclass loginLogic(QMainWindow,loginUI):def __init__(self):super(loginLogic, self).__init__()self.setupUi(self)self.setWindowIcon(QIcon("datas/icon.ico"))self.CSTLogic = CSTLogic()self.run()# 按钮绑定事件def run(self):# 登录self.pushButton.clicked.connect(self.login_)# 注册self.pushButton_2.clicked.connect(self.register_)# 登录事件def login_(self):pass# 注册事件def register_(self):pass# 重新启动界面def reshow(self):self.show()# 运行界面循环
def main_():app = QApplication(sys.argv)login_ = loginLogic()login_.show()sys.exit(app.exec_())if __name__ == '__main__':main_()
三、主界面comsumeType.py
四、主界面逻辑代码comsumeTypeLogic.py
from comsumeType import Ui_MainWindow as CSTUIfrom PyQt5.QtWidgets import QMessageBox,QMainWindow,QApplication
from PyQt5.QtGui import QIcon
from PyQt5 import QtCoreimport sysclass comsumeTypeLogic(QMainWindow,CSTUI):returnLoginSignal = QtCore.pyqtSignal()def __init__(self):super(comsumeTypeLogic, self).__init__()self.setupUi(self)self.setWindowIcon(QIcon("datas/icon.ico"))self.run()# 按钮绑定事件def run(self):# 退出系统self.pushButton_10.clicked.connect(self.exitSystem)# 返回登录界面self.pushButton_11.clicked.connect(self.returnLogin)# 退出系统def exitSystem(self):sys.exit()# 返回登录界面def returnLogin(self):self.close()self.returnLoginSignal.emit()if __name__ == '__main__':app =QApplication(sys.argv)CSTLogic = comsumeTypeLogic()CSTLogic.show()sys.exit(app.exec_())
五、登录界面跳转至主界面
1、在登录界面逻辑代码loginLogic.py中将主界面的逻辑代码实例化
self.CSTLogic = CSTLogic()
2、登录界面逻辑代码loginLogic.py中需要跳转的位置插入以下代码
跳转到主界面同时关闭登录界面
self.CSTLogic.show()# 登录界面关闭self.close()
六、主界面回到登录界面
1、在主界面逻辑代码comsumeTypeLogic.py中,添加信号
returnLoginSignal = QtCore.pyqtSignal()
2、在主界面逻辑代码comsumeTypeLogic.py中需要跳转到登录界面的地方触发信号并发射
# 返回登录界面def returnLogin(self):self.close()self.returnLoginSignal.emit()
3、在登录界面逻辑代码loginLogic.py中实例化主界面逻辑代码,且将信号绑定槽函数
self.CSTLogic.show()
self.CSTLogic.returnLoginSignal.connect(self.reshow)
4、在登录界面逻辑代码loginLogic.py中编写槽函数代码
def reshow(self):self.show()