Python pyqt interface jump explanation/notes

Tips: There are many such articles on the Internet, I am dazzled to see them, besides being dumbfounded or dumbfounded, then I copy their code, run it, and read it slowly. Now I have a little bit of it, and I understand it, so I write this article. The blog serves as study notes. The blogger is a complete novice, please point out any mistakes, thank you~(⊙o⊙)

Please point out the error, thank you! ! !

Not much nonsense, just start

import openpyxl

import sys
from PyQt5.QtWidgets import (QWidget, QToolTip, QDesktopWidget, QMessageBox, QTextEdit, QLabel,
                             QPushButton, QApplication, QMainWindow, QAction, qApp, QHBoxLayout, QVBoxLayout,
                             QGridLayout,
                             QLineEdit,QDialog)
from PyQt5.QtGui import QFont, QIcon, QIntValidator
from PyQt5.QtCore import QCoreApplication

'''00000001 12345678用户账号密码
   000001 123456管理员账号密码
   00001  12345老板账号密码
'''
acpalist = [{
    
    '00001': '12345'}, {
    
    '000001': '123456'}, {
    
    '0000001': '1245678'}]


class login(QWidget):
    def __init__(self):
        super().__init__()
        #self.loginbtn = QPushButton('登录',self)
        self.initUI()


    # ---------------------------登录界面------------------------------------
    def initUI(self):

        # 两个按钮
        loginbtn = QPushButton('登录',self)
        loginbtn.clicked.connect(loginevent)#信号槽机制——鼠标点击后执行loginevent函数
        forgetbtn = QPushButton('忘记密码',self)


        # 两个标签
        account = QLabel('账号', self)
        password = QLabel('密码', self)

        global qleaccount, qlepassword
        # 两个输入框,用这些输入框对象的方法去实现限数字输入,密文输入
        qleaccount = QLineEdit(self)
        qleaccount.setValidator(QIntValidator())  # 限制账号为整数int型
        qlepassword = QLineEdit(self)
        qlepassword.setEchoMode(qlepassword.Password)  # 密文输入,密码用圆点掩饰


        # 使用箱布局让界面更好看,move()布局是绝对定位,不好看
        haccbox = QHBoxLayout()
        haccbox.addStretch(1)
        haccbox.addWidget(account)
        haccbox.addWidget(qleaccount)
        haccbox.addStretch(1)

        hpassbox = QHBoxLayout()
        hpassbox.addStretch(1)
        hpassbox.addWidget(password)
        hpassbox.addWidget(qlepassword)
        hpassbox.addStretch(1)

        hbtnbox = QHBoxLayout()
        hbtnbox.addStretch(1)
        hbtnbox.addWidget(loginbtn)
        hbtnbox.addWidget(forgetbtn)
        hbtnbox.addStretch(1)

        vbox = QVBoxLayout()
        vbox.addStretch(1)
        vbox.addLayout(haccbox)
        vbox.addLayout(hpassbox)
        vbox.addStretch(1)
        vbox.addLayout(hbtnbox)
        vbox.addStretch(1)

        self.setLayout(vbox)
        self.setGeometry(900, 400, 100, 90)
        self.setWindowTitle('ATM登录')
        self.setWindowIcon(QIcon('logo.jpg'))

class user(QDialog):#必须是Dialog不能是QWidget才能实现界面跳转
    def __init__(self):
        super().__init__()
        self.initUI()

    # ----------------------用户界面------------------------
    def initUI(self):
        # 欢迎标签
        qlwelcome = QLabel('欢迎', self)
        # qlaccnumber = QLabel(account_number, self)

        # 按钮控件
        moneyinbtn = QPushButton('存款')
        moneyoutbtn = QPushButton('取款')
        paybtn = QPushButton('转账')
        recordlookbtn = QPushButton('消费记录查询')
        moneyinbtn.clicked.connect(testevent)
        # 箱布局
        # 欢迎
        welhbox = QHBoxLayout()
        welhbox.addWidget(qlwelcome)
        # welhbox.addWidget(qlaccnumber)
        welhbox.addStretch(1)
        # 四个按钮排版
        v4btnbox = QVBoxLayout()
        v4btnbox.addStretch(1)
        v4btnbox.addWidget(moneyinbtn)
        v4btnbox.addWidget(moneyoutbtn)
        v4btnbox.addWidget(paybtn)
        v4btnbox.addWidget(recordlookbtn)
        v4btnbox.addStretch(1)

        h4btnbox = QHBoxLayout()
        h4btnbox.addStretch(1)
        h4btnbox.addLayout(v4btnbox)
        h4btnbox.addStretch(1)

        # 最后排版
        vfinallybox = QVBoxLayout()
        vfinallybox.addLayout(welhbox)
        vfinallybox.addLayout(h4btnbox)

        self.setLayout(vfinallybox)  # 放置

        self.setGeometry(1000, 600, 600, 300)
        self.setWindowTitle('user')
        self.setWindowIcon(QIcon('logo.jpg'))
class test(QDialog):
    def __init__(self):
        super().__init__()
        self.initUI
    def initUI(self):
        self.setGeometry(1000,800,600,400)
def testevent():
    test1.show()
def loginevent():
    for i in acpalist:
        tacc = list(i.items())[0][0]
        tpas = list(i.items())[0][1]
        if qleaccount.text() == tacc and len(tacc) == 5 and tpas == qlepassword.text() :
            login1.close()
            user1.show()




app = QApplication(sys.argv)

login1 = login()
user1 = user()
test1 = test()
login1.show()



sys.exit(app.exec_())

Don't try to understand my long and ugly code first (my comments are incomplete) I will slowly lead you to interpret it~~

I admit, it may be a bit long. You can’t read the long one. It’s okay. Just copy and paste and run. First, you enter 00001 in the login interface and enter 12345 in the password, click login, ok, and the interface you want will jump. , No, it's not over yet, you click deposit and jump again! !


Alright, let’s get to the point!!!

If you want to realize the jump of three interfaces, you must write three interfaces!
The above code of mine wrote three interface login interface (login), user interface (user), and a test interface (test)

A single interface is written as a class.

Note: The parent class of the login class is QWidget and the parent class of the user and test classes is Dialog. This is very important! ! ! I don’t know why, I dare not ask (please comment on the whole district to explain)

Then my code is set to click the button to jump to the interface, where the signals and slots in pyqt are used

In the brackets of btn.clicked.connect() is the triggered event (it can be a function defined by yourself). I have defined a new function loginvent here. Now go back and look at this function defined in my code.
You can see the content of my function emmm...Don't spray me, I set up an account and password match, you can write it directly

def loginevent():
	login1.colse()#关闭login1登录界面
	user.show()#打开user用户界面

For the user category, the
deposit button btn.clicked.connect()
also sets an event (still a function defined by myself, of course you can directly write test1.show)

Hey hey, look, right? Can the interface jump~~
Have you learned

If you have any questions, please put it in the comment area~~

Guess you like

Origin blog.csdn.net/qq_51182221/article/details/112795242