演示
pyqt5实现登录界面、主界面相互跳转
功能
1、登录:登陆后跳转主界面
2、登出:登出后跳转登录界面
编程思路
1、使用designer设计界面生成ui文件。命令行执行pyuic5 -o xxx.py xxx.ui实现.ui转.py。designer位于qt5_applications\Qt\bin
2、设计模块代码架构。使模块具备更强的可读性,可编写性、拓展性
3、新类继承py文件中的ui类,编写相关槽函数或其他功能性实例函数
运行教程+源代码
pip install pyqt5 pip install pyqt5-tools
main.py
from login import Ui_Dialog from PyQt5.QtWidgets import * from PyQt5.QtGui import * from index import MainUI class Main(Ui_Dialog): def __init__(self): super(Main, self).__init__() self.window = QMainWindow() self.setupUi(self.window) self.pushButton.clicked.connect(self.logining) self.window.show() def logining(self): self.main_ui = MainUI(self) self.main_ui.pushButton.clicked.connect(self.loginout) self.window.close() self.main_ui.mainwindow.show() def loginout(self): self.main_ui.mainwindow.close() self.window.show() if __name__ == '__main__': app = QApplication([]) stats = Main() app.exec_()
login.py
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file '11.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, QtWidgets import os import qt5_applications dirname = os.path.dirname(qt5_applications.__file__) plugin_path = os.path.join(dirname, 'Qt', 'plugins', 'platforms') os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path class Ui_Dialog(object): def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(400, 277) self.pushButton = QtWidgets.QPushButton(Dialog) self.pushButton.setGeometry(QtCore.QRect(120, 170, 75, 23)) self.pushButton.setStyleSheet("") self.pushButton.setObjectName("pushButton") self.pushButton_2 = QtWidgets.QPushButton(Dialog) self.pushButton_2.setGeometry(QtCore.QRect(210, 170, 75, 23)) self.pushButton_2.setObjectName("pushButton_2") self.lineEdit = QtWidgets.QLineEdit(Dialog) self.lineEdit.setGeometry(QtCore.QRect(120, 60, 161, 21)) self.lineEdit.setStyleSheet("") self.lineEdit.setObjectName("lineEdit") self.lineEdit_2 = QtWidgets.QLineEdit(Dialog) self.lineEdit_2.setGeometry(QtCore.QRect(120, 110, 161, 21)) self.lineEdit_2.setObjectName("lineEdit_2") self.retranslateUi(Dialog) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "Dialog")) self.pushButton.setText(_translate("Dialog", "PushButton")) self.pushButton_2.setText(_translate("Dialog", "PushButton"))
index.py
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'index.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, QtWidgets from PyQt5.QtWidgets import * from PyQt5.QtGui import * class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(800, 600) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(700, 20, 75, 23)) self.pushButton.setObjectName("pushButton") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) 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.pushButton.setText(_translate("MainWindow", "PushButton")) class MainUI(Ui_MainWindow): def __init__(self,window): super(MainUI, self).__init__() self.mainwindow=QMainWindow() self.setupUi(self.mainwindow)
云盘链接:https://pan.baidu.com/s/18BwnxE3NtBxJuPk_90vikA
提取码:s6ai