Make the login interface in Python

PyQt5 is a high-level language that displays a small window with just a few lines of code. The basic functions of Windows have been implemented at the bottom layer.

Login interface:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import*
from PyQt5.QtCore import*
from Dialog.HttpLogin import HttpPost

class Ui_dialog(QWidget) :

    def __init__(self) :
        super().__init__()
        self.setupUi() Leave interface rendering to InitUi

    def setupUi(self) :
        self.setObjectName("Student Login Interface")
        self.resize(535.344)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(14)

        Note the order in which variables are defined
        self.lineEdit = QtWidgets.QLineEdit(self)
        self.lineEdit.setGeometry(QtCore.QRect(190.100.231.31))
        self.lineEdit.setObjectName("lineEdit")
        Set it to the password input box
        self.lineEdit_2 = QtWidgets.QLineEdit(self)
        self.lineEdit_2.setEchoMode(QtWidgets.QLineEdit.Password)
        self.lineEdit_2.setGeometry(QtCore.QRect(190.150.231.31))
        self.lineEdit_2.setObjectName("lineEdit_2")

        # disable window changes (top Settings)
        self.setWindowFlags(QtCore.Qt.WindowCloseButtonHint)
        self.setFont(font)
        self.pushButton = QtWidgets.QPushButton(self)
        self.pushButton.setGeometry(QtCore.QRect(100.280.93.28))
        self.pushButton.setObjectName("pushButton")
        Add a response event to the cancel button
        self.pushButton.clicked.connect(QCoreApplication.instance().quit)

        # confirm button
        self.pushButton_2 = QtWidgets.QPushButton(self)
        self.pushButton_2.setGeometry(QtCore.QRect(340.280.93.28))
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_2.clicked.connect(self.forlogin)No parameters click Execute

        self.label = QtWidgets.QLabel(self)
        self.label.setGeometry(QtCore.QRect(90.100.72.21))

        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self)
        self.label_2.setGeometry(QtCore.QRect(90.155.72.20))
        self.label_2.setObjectName("label_2")

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

    def retranslateUi(self, dialog) :
        _translate = QtCore.QCoreApplication.translate
        dialog.setWindowTitle(_translate("dialog"."Dialog"))
        self.pushButton.setText(_translate("dialog"."Cancel"))
        self.pushButton_2.setText(_translate("dialog"."Confirm"))
        self.label.setText(_translate("dialog"."Account number:"))
        self.label_2.setText(_translate("dialog"."Password:"))



    def forlogin(self) :
       Get the class capacity of the account input box and the password input box
        name = self.lineEdit.text()
        password = self.lineEdit_2.text()
        sign = HttpPost.login(name,password)
        if sign=="true":
            print("Login successful")
            self.QCoreApplication.instance().quitClose the current window
        else:
            print("Login failed")
            reply = QtWidgets.QMessageBox.question(self,"Warning"."Incorrect password. Please log in again.",QtWidgets.QMessageBox.Yes, QtWidgets.QMessageBox.No)
            if reply == QtWidgets.QMessageBox.Yes:
                self.lineEdit_2.setText("")
                self.lineEdit.setText("")
            else:
                self.QCoreApplication.instance().quitClose the current window



if __name__ == '__main__':
    app = QApplication(sys.argv)
    my=Ui_dialog()
    my.show()
    sys.exit(app.exec_())
Copy the code

Access the server through the URL to parse the response JSON

import json
import tkinter # to display text and images
import urllib
class HttpPost() :
    def login(name,password) :
        print("Perform login operation")
        url = "http://localhost:8080/login"
        try:
            data = bytes(urllib.parse.urlencode({'name': str(name),'password':str(password)}), encoding=
            'utf8')
            response = urllib.request.urlopen(url,data=data)
            jsonDatas = json.load(response)
            sign =jsonDatas["sign"]   If the transmission is in class form, it is resolved through a 2-layer structure
            return sign
        except (SyntaxError) as err:
            print("SyntaxError"+err.args)
        finally:
            None;
Copy the code

The running effect is as follows: