当前位置:网站首页>(6) Pyqt5--- > window jump (registration login function)

(6) Pyqt5--- > window jump (registration login function)

2022-06-25 12:36:00 haoming Hu

GitHub Connect :
This column all source code GitHub Through train

This column all source code Gitte Address

Another way to connect signals and slots was mentioned in the previous article

This blog was originally 2020 It should be released by the end of the year , Until now ! In the spare time of review , It's really enjoyable to be able to write something comfortable

This time, the main content is the interface jump to realize the login function

My method is clumsy , But it works . In fact, as long as one thing is completed, it will be completely OK 了 : When your login button is click When time is triggered , The slot function you connect to should be your own login API, Of course, this time I use the login interface function of my own server , When you use it, change it to your own login API Can , Come back , If you're successful , You need to send messages to each of the two interfaces , Make the action of closing this window and opening a new one , I'm used to writing slot functions in my own class , In this way, you can't pass information to another window object ( I haven't thought of a good way for the time being ), So I write this slot function directly in the main thread , Then the button binding event is also bound after the slot function .

I used to write tkinter That's what I did when I was young , There's nothing wrong at the moment , It can be said to be simple and rude

This code file mainly has

  • login_ui.py
  • main_ui.py
  • main.py

The first two are ui Turning around py file ,main.py A file is a file in which a program runs

If you don't want to go GitHub If you download the code above , I just posted it

login_ui.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'login.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# 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


class Ui_login(object):
    def setupUi(self, login):
        login.setObjectName("login")
        #login.setStyleSheet("#login{background-color:green}")
        login.resize(412, 269)
        self.id_label = QtWidgets.QLabel(login)
        self.id_label.setGeometry(QtCore.QRect(110, 60, 51, 41))
        font = QtGui.QFont()
        font.setFamily("Agency FB")
        font.setPointSize(14)
        self.id_label.setFont(font)
        self.id_label.setObjectName("id_label")
        self.pw_label_2 = QtWidgets.QLabel(login)
        self.pw_label_2.setGeometry(QtCore.QRect(110, 120, 51, 41))
        font = QtGui.QFont()
        font.setFamily("Agency FB")
        font.setPointSize(14)
        self.pw_label_2.setFont(font)
        self.pw_label_2.setObjectName("pw_label_2")
        self.login_pushButton = QtWidgets.QPushButton(login)
        self.login_pushButton.setGeometry(QtCore.QRect(100, 200, 75, 23))
        self.login_pushButton.setObjectName("login_pushButton")
        self.exit_pushButton = QtWidgets.QPushButton(login)
        self.exit_pushButton.setGeometry(QtCore.QRect(240, 200, 75, 23))
        self.exit_pushButton.setObjectName("exit_pushButton")
        self.lineEdit_id = QtWidgets.QLineEdit(login)
        self.lineEdit_id.setGeometry(QtCore.QRect(170, 70, 113, 20))
        self.lineEdit_id.setObjectName("lineEdit_id")
        self.lineEdit_pw = QtWidgets.QLineEdit(login)
        self.lineEdit_pw.setGeometry(QtCore.QRect(170, 130, 113, 20))
        self.lineEdit_pw.setObjectName("lineEdit_pw")
        self.lineEdit_pw.setEchoMode(QtWidgets.QLineEdit.Password)
        self.retranslateUi(login)
        self.exit_pushButton.clicked.connect(login.close)
        QtCore.QMetaObject.connectSlotsByName(login)

    def retranslateUi(self, login):
        _translate = QtCore.QCoreApplication.translate
        login.setWindowTitle(_translate("login", "Form"))
        self.id_label.setText(_translate("login", " account number "))
        self.pw_label_2.setText(_translate("login", " password "))
        self.login_pushButton.setText(_translate("login", " Sign in "))
        self.exit_pushButton.setText(_translate("login", " sign out "))

main_ui.py file

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'main.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# 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


class Ui_main(object):
    def setupUi(self, main):
        main.setObjectName("main")
        main.resize(1007, 631)

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

    def retranslateUi(self, main):
        _translate = QtCore.QCoreApplication.translate
        main.setWindowTitle(_translate("main", "Form"))

main.py file

""" /******************************************************************************** * @Filename: main.py * @Author: haomingHu * @Version: 1.0 * @Date: 2021-03-26 * @Description: * @History: ********************************************************************************/ """



from login_ui import Ui_login
from main_ui  import Ui_main
import sys
from PyQt5 import QtWidgets
from PyQt5.QtGui import QPalette,QBrush,QPixmap
from PyQt5.QtCore import Qt

class myLogin(QtWidgets.QWidget,Ui_login):
    login_state = 0 
    def __init__(self):
        super(myLogin,self).__init__()
        self.setupUi(self)
        loginPalette = QPalette()
        loginPalette.setBrush(QPalette.Background, QBrush(QPixmap("./loginbackground.jpg")))
        #loginPalette.setColor(QPalette.Background, Qt.blue)
        self.setPalette(loginPalette)

class myMainGui(QtWidgets.QWidget,Ui_main):
    userID = ""
    # Constructors 
    def __init__(self):
        super(myMainGui,self).__init__()
        self.setupUi(self)

    def closeEvent(self,event):# The function name is fixed and immutable 
        reply=QtWidgets.QMessageBox.question(self,u'Notice!',u'Are you sure to exit?'
            ,QtWidgets.QMessageBox.Yes,QtWidgets.QMessageBox.No)
        #QtWidgets.QMessageBox.question(self,u' Pop up window name ',u' Pop-up content ', Options 1, Options 2)
        if reply==QtWidgets.QMessageBox.Yes:
            event.accept()# close window 
        else:
            event.ignore()# Ignore the click X event 



if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    myLogin = myLogin()
    myLogin.show()
    mainGUI = myMainGui()
    
    userIDSure = ""
    flag = 0
    def login_check():
        userID = myLogin.lineEdit_id.text()
        user_PW = myLogin.lineEdit_pw.text()
        print(userID)
        print(user_PW)
        '''  Here I use the login request interface of my server   When you use it, you can replace it with your own encapsulated login verification interface  '''
        
        '''  Normally , If you log in successfully , You need to send the successful login user information back to the main interface   That is to tell the main interface , Which user logged in , At this time, it can be defined in the class of the main interface   Some class properties , If login is successful , Will lineedit The input content of the object can be assigned to the class property  '''
        #login_flag = event.loginRequest(userID,user_PW)
        #if login_flag == 0:
        if userID == "1" and user_PW == "1":
            myLogin.login_state = 1
            mainGUI.userID = myLogin.lineEdit_id.text()
            myLogin.close()
            mainGUI.show()
            
    myLogin.login_pushButton.clicked.connect(login_check)

    sys.exit(app.exec_())

 Insert picture description here

Supplementary information :

  • obtain lineedit How to input content :

Use lineedit Of text Method to get , The object obtained is a string object .

        userID = myLogin.lineEdit_id.text()
        user_PW = myLogin.lineEdit_pw.text()
  • Set the background ( I'll write a blog later )
  1. Window objects come with setStyleSheet Method
​    login.setObjectName("login")

​    login.setStyleSheet("#login{background-color:green}")

​ 2、 Use QPalette class

window = QMainWindow()
palette = QPalette()
palette.setColor(QPalette.Background, Qt.red)
window.setPalette(palette)

I wish you all a happy life , Happy learning !

原网站

版权声明
本文为[haoming Hu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200528576852.html

随机推荐