PyQt5——软件密码登陆跳转主界面

软件启动前输入密码,输入正确才能进入主界面,登陆窗口如下图所示:

 很自然的一个思路就是判断文本框内的值是否等于预设密码(12345),如果正确则显示主页面,代码如下:

# ——创建时间:2019.2.19——
# 需要密码登陆软件,登陆成功后跳转主界面

import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import qdarkstyle
import time
# 登陆对话框
class DialogUI(QWidget):
    def __init__(self,parent=None):
        super(DialogUI,self).__init__(parent)

        self.setWindowTitle("登陆")
        # self.resize(350,150)
        # 输入密码框
        flo=QFormLayout()
        e1=QLineEdit()
        BtnOk=QPushButton("  确   定   ")
        BtnCancel=QPushButton(" 取 消 ")
        BtnCancel.clicked.connect(self.close)   # 点击取消关闭窗口
        e1.setEchoMode(QLineEdit.Password)  # 设置密码不可见
        e1.textChanged.connect(self.textchanged)
        flo.addRow("请输入密码:",e1)
        flo.addRow(BtnOk,BtnCancel)
        self.setLayout(flo)

    # 核对密码是否正确
    def textchanged(self,text):
        if text == "12345":
            self.close()    # 关闭登陆界面
            Main()
            print("输入正确,跳转至主界面")

class UI_Main(QWidget):
    def __init__(self):
        super(UI_Main, self).__init__()
        self.setWindowTitle('主窗口')

def Main():
    app = QApplication(sys.argv)
    u = UI_Main()
    u.show()
    time.sleep(10000)
    sys.exit(app.exec())

if __name__ == "__main__":
    app = QApplication(sys.argv)
    d = DialogUI()
    d.show()
    # Main()
    # 使用qdarkstyle渲染模式
    app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
    sys.exit(app.exec())

运行主窗口会闪退,加入延时后,主窗口会出现“未相应”的bug,如图

 结束程序时显示

Process finished with exit code -1

 说明这样子进行是有问题的,再想办法。


单独调试时发现时这句话放在主窗口显示当中会出现bug

    time.sleep(10000)

如果不加上,主窗口会出现闪退状况


原来之前时忘记将主窗口实例化了,犯了低级错误,现在解决所有bug,输入密码12345既可以打开主窗口,完整程序如下

# ——创建时间:2019.2.19——
# 需要密码登陆软件,登陆成功后跳转主界面

import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import qdarkstyle
import time
12345
# 登陆对话框
class DialogUI(QWidget):
    def __init__(self,parent=None):
        super(DialogUI,self).__init__(parent)

        self.setWindowTitle("登陆")
        # self.resize(350,150)
        # 输入密码框
        flo=QFormLayout()
        e1=QLineEdit()
        BtnOk=QPushButton("  确   定   ")
        BtnCancel=QPushButton(" 取 消 ")
        BtnCancel.clicked.connect(self.close)   # 点击取消关闭窗口
        e1.setEchoMode(QLineEdit.Password)  # 设置密码不可见
        e1.textChanged.connect(self.textchanged)
        flo.addRow("请输入密码:",e1)
        flo.addRow(BtnOk,BtnCancel)
        self.setLayout(flo)

    # 核对密码是否正确
    def textchanged(self,text):
        if text == "12345":
            self.close()    # 关闭登陆界面
            WindowShow.show()
            print("输入正确,跳转至主界面")

# 主窗口
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1274, 860)
class WindowShow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(WindowShow, self).__init__()
        self.setupUi(self)
        self.setWindowTitle('主窗口')
        self.setWindowIcon(QIcon('icon.png'))


if __name__ == "__main__":
    app = QApplication(sys.argv)
    d = DialogUI()
    d.show()
    WindowShow=WindowShow() # 生成主窗口的实例
    # 使用qdarkstyle渲染模式
    app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
    sys.exit(app.exec())


PS:其实因为这个信号连接是自动发射的,所以其实输入完密码不用按下确定键自动就会跳转

扫描二维码关注公众号,回复: 5311437 查看本文章

主要的问题解决了,后面可以加上密码长度限定、密码提示、密码出错次数过多锁定等功能

猜你喜欢

转载自blog.csdn.net/qq_41895747/article/details/87727460