PyQt5的第一个小项目:登录跳转页面

  1. 首先创建一个主窗口,命名为Login.py
    注:这一步可以通过QTDesigner工具来画出图形,然后生成.py文件,再做修改
    如何生成.py文件的过程已在上一篇博客发布,谢谢关注
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
	#定义一个建立UI界面的函数
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")   #设置窗口名称
        MainWindow.resize(386, 127)  #设置窗口大小
        self.centralWidget = QtWidgets.QWidget(MainWindow)
        self.centralWidget.setObjectName("centralWidget")
        MainWindow.setCentralWidget(self.centralWidget)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))  #设置主窗口名称


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

运行的结果:
PyQt5的第一个小项目:登录跳转页面_第1张图片

  1. 可以美化界面,添加logo和背景图
from PyQt5.QtGui import QIcon
        MainWindow.setWindowIcon(QIcon('logo.png'))
        MainWindow.setStyleSheet("background-image:url(Background.jpg)")

PyQt5的第一个小项目:登录跳转页面_第2张图片

  1. 到这一步的所有代码
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(386, 127)
        MainWindow.setWindowIcon(QIcon('logo.png'))
        MainWindow.setStyleSheet("background-image:url(Background.jpg)")
        self.centralWidget = QtWidgets.QWidget(MainWindow)
        self.centralWidget.setObjectName("centralWidget")
        MainWindow.setCentralWidget(self.centralWidget)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "一颗数据小白菜"))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
  1. 添加登录界面的组件
    2个QLable,显示账号和密码
    2个LineEdit,提示输入账号和密码
    2个QPushButton,绑定事件触发
self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(200,24,24,12))
        self.label.setTextFormat(QtCore.Qt.AutoText)
        self.label.setObjectName("label")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(250,20, 100, 20))
        self.lineEdit.setText("")
        self.lineEdit.setObjectName("lineEdit")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(200, 54, 24, 12))
        self.label_2.setObjectName("label_2")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_2.setGeometry(QtCore.QRect(250,50,100,20))
        self.lineEdit_2.setText("")
        self.lineEdit_2.setEchoMode(QtWidgets.QLineEdit.Password)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.pushButtonOK = QtWidgets.QPushButton(self.centralwidget)
        self.pushButtonOK.setGeometry(QtCore.QRect(190,90, 75, 23))
        self.pushButtonOK.setObjectName("pushButtonOK")
        self.pushButtonCancel = QtWidgets.QPushButton(self.centralwidget)
        self.pushButtonCancel.setGeometry(QtCore.QRect(290, 90, 75, 23))
        self.pushButtonCancel.setObjectName("pushButtonCancel")
        MainWindow.setCentralWidget(self.centralwidget)
  1. 定义一个密码验证的函数
    def user_login(self):
        usr_name = self.lineEdit.text()
        usr_pwd = self.lineEdit_2.text()
        if usr_name=='geekori' and usr_pwd=='1234':
            QMessageBox.information(self,'消息','登录成功')
            #ui_hello = hello_mainWindow()
            ui_hello.show()
            MainWindow.close()
        else:
            #QMessageBox.information(self, '消息', '账号或密码错误')
            QMessageBox.warning(self,
                                "警告",
                                "用户名或密码错误!",
                                QMessageBox.Yes)
            self.lineEdit.setFocus() #如果密码错误,重新定位到输入栏
  1. 定义一个登录成功的跳转的页面hello.py,该代码在Login.py的同级目录下
from PyQt5 import QtCore,QtGui,QtWidgets

class hello_mainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(hello_mainWindow,self).__init__()
        self.setupUi(self)
        self.retranslateUi(self)
    def setupUi(self,mainWindow):
        mainWindow.setObjectName("mainWindow")
        mainWindow.setWindowModality(QtCore.Qt.WindowModal)
        mainWindow.resize(624,511)
        self.retranslateUi(mainWindow)
        QtCore.QMetaObject.connectSlotsByName(mainWindow)
    def retranslateUi(self,mainWindow):
        _translate=QtCore.QCoreApplication.translate
        mainWindow.setWindowTitle(_translate("mainWindow","hello world"))

if __name__=="__main__":
    import sys
    app=QtWidgets.QApplication(sys.argv)
    mainWindow=QtWidgets.QMainWindow()
    ui=hello_mainWindow()
    ui.setupUi(mainWindow)
    mainWindow.show()
    sys.exit(app.exec_())
  1. 绑定确定和取消按钮的事件
pushButtonOK.clicked.connect(self.user_login)
pushButtonCancel.clicked.connect(MainWindow.close)
  1. 最后,完整的Login.py文件如下
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from hello import *

class Ui_MainWindow(QtWidgets.QMainWindow):

    def __init__(self):
        super(Ui_MainWindow,self).__init__()
        self.setupUi(self)
        self.retranslateUi(self)

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(386, 127)
        MainWindow.setWindowIcon(QIcon('pic.png'))
        MainWindow.setStyleSheet("background-image:url(face2.jpg)")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(200,24,24,12))
        self.label.setTextFormat(QtCore.Qt.AutoText)
        self.label.setObjectName("label")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(250,20, 100, 20))
        self.lineEdit.setText("")
        self.lineEdit.setObjectName("lineEdit")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(200, 54, 24, 12))
        self.label_2.setObjectName("label_2")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_2.setGeometry(QtCore.QRect(250,50,100,20))
        self.lineEdit_2.setText("")
        self.lineEdit_2.setEchoMode(QtWidgets.QLineEdit.Password)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.pushButtonOK = QtWidgets.QPushButton(self.centralwidget)
        self.pushButtonOK.setGeometry(QtCore.QRect(190,90, 75, 23))
        self.pushButtonOK.setObjectName("pushButtonOK")
        self.pushButtonCancel = QtWidgets.QPushButton(self.centralwidget)
        self.pushButtonCancel.setGeometry(QtCore.QRect(290, 90, 75, 23))
        self.pushButtonCancel.setObjectName("pushButtonCancel")
        MainWindow.setCentralWidget(self.centralwidget)

        self.pushButtonOK.clicked.connect(self.user_login)
        self.pushButtonCancel.clicked.connect(MainWindow.close)

        # self.menubar = QtWidgets.QMenuBar(MainWindow)
        # self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
        # 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", "一颗数据小白菜"))
        self.lineEdit.setPlaceholderText(_translate("MainWindow", "请输入帐号"))
        self.lineEdit_2.setPlaceholderText(_translate("MainWindow", "请输入密码"))
        self.label.setText(_translate("MainWindow", "账号"))
        self.label_2.setText(_translate("MainWindow", "密码"))
        self.pushButtonOK.setText(_translate("MainWindow", "确定"))
        self.pushButtonCancel.setText(_translate("MainWindow", "取消"))
   
    def user_login(self):
        usr_name = self.lineEdit.text()
        usr_pwd = self.lineEdit_2.text()
        if usr_name=='geekori' and usr_pwd=='1234':
            QMessageBox.information(self,'消息','登录成功')
            #ui_hello = hello_mainWindow()
            ui_hello.show()
            MainWindow.close()
        else:
            #QMessageBox.information(self, '消息', '账号或密码错误')
            QMessageBox.warning(self,
                                "警告",
                                "用户名或密码错误!",
                                QMessageBox.Yes)
            self.lineEdit.setFocus()


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

PyQt5的第一个小项目:登录跳转页面_第3张图片
PyQt5的第一个小项目:登录跳转页面_第4张图片

这里还有个问题,就是我想把login.py作为一个模块在另一个程序中调用,但是在调用的过程中生成的跳转页面会闪退,不知道为啥,如果在主程序文件下调用则不会出现这个问题,希望有知道的朋友给个解答,非常感谢

你可能感兴趣的:(pyhon,GUI框架,qt,python,ui)