目录
1、设计ui界面
2、设计逻辑代码,实现登录界面跳转
3、结果
1、设计ui界面
设计后的ui界面
在这里可以设置密码不显示
这里可以设置快捷键
最后将ui界面转为py文件后获得的逻辑代码为:(文件名为Login.py)
# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'longui.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(383, 208)self.centralwidget = QtWidgets.QWidget(MainWindow)self.centralwidget.setObjectName("centralwidget")self.pushButton = QtWidgets.QPushButton(self.centralwidget)self.pushButton.setGeometry(QtCore.QRect(150, 140, 75, 23))self.pushButton.setObjectName("pushButton")self.widget = QtWidgets.QWidget(self.centralwidget)self.widget.setGeometry(QtCore.QRect(100, 50, 41, 61))self.widget.setObjectName("widget")self.verticalLayout = QtWidgets.QVBoxLayout(self.widget)self.verticalLayout.setContentsMargins(0, 0, 0, 0)self.verticalLayout.setObjectName("verticalLayout")self.label = QtWidgets.QLabel(self.widget)self.label.setObjectName("label")self.verticalLayout.addWidget(self.label)self.label_2 = QtWidgets.QLabel(self.widget)self.label_2.setObjectName("label_2")self.verticalLayout.addWidget(self.label_2)self.widget1 = QtWidgets.QWidget(self.centralwidget)self.widget1.setGeometry(QtCore.QRect(140, 50, 131, 61))self.widget1.setObjectName("widget1")self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.widget1)self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)self.verticalLayout_2.setObjectName("verticalLayout_2")self.lineEdit = QtWidgets.QLineEdit(self.widget1)self.lineEdit.setObjectName("lineEdit")self.verticalLayout_2.addWidget(self.lineEdit)self.lineEdit_2 = QtWidgets.QLineEdit(self.widget1)self.lineEdit_2.setEchoMode(QtWidgets.QLineEdit.Password)self.lineEdit_2.setObjectName("lineEdit_2")self.verticalLayout_2.addWidget(self.lineEdit_2)MainWindow.setCentralWidget(self.centralwidget)self.retranslateUi(MainWindow)QtCore.QMetaObject.connectSlotsByName(MainWindow)def retranslateUi(self, MainWindow):_translate = QtCore.QCoreApplication.translateMainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))self.pushButton.setText(_translate("MainWindow", "登录"))self.pushButton.setShortcut(_translate("MainWindow", "Return")) # 设置快捷键self.label.setText(_translate("MainWindow", "账号:"))self.label_2.setText(_translate("MainWindow", "密码:"))
2、设计逻辑代码,实现登录界面跳转
这里登录后,我们转到另外一个窗口。这里我们结合以下文章实现界面跳转:Pyqt5打开电脑摄像头进行拍照_清纯世纪的博客-CSDN博客。
逻辑代码如下:(文件名为Login_mian.py)
from Login import Ui_MainWindow
from Camera_main import *class login_window(QtWidgets.QMainWindow, Ui_MainWindow):def __init__(self):super(login_window, self).__init__()self.setupUi(self) # 创建窗体对象self.init()self.admin = "123456"self.Password = "123456"def init(self):self.pushButton.clicked.connect(self.login_button) # 连接槽def login_button(self):if self.lineEdit.text() == "":QMessageBox.warning(self, '警告', '账号不能为空,请输入!')return Noneif self.lineEdit_2.text() == "":QMessageBox.warning(self, '警告', '密码不能为空,请输入!')return Noneif (self.lineEdit.text() == self.admin) and self.lineEdit_2.text() == self.Password:# 1打开新窗口Ui_Main.show()# 2关闭本窗口self.close()else:QMessageBox.critical(self, '错误', '账号或密码错误!')self.lineEdit.clear()return Noneif __name__ == '__main__':from PyQt5 import QtCoreQtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) # 自适应分辨率app = QtWidgets.QApplication(sys.argv)window = login_window()Ui_Main = Open_Camera() # 生成主窗口的实例window.show()sys.exit(app.exec_())
3、结果
注意:两个界面的函数不能一样,因为一般没有更改的用PyUIC直接生成的.py文件的函数名是一样的,要进行更改。
参考:暂未成功人士