pyqt5 登录界面的实现模板(加强版2)

简介: 本例,在[登录界面第二版](https://yq.aliyun.com/articles/653646)的基础上,增加了登录界面的记住密码功能和自动登录功能。 在实现这两个功能的时候,需要用到QSettings这个知识点。QSettings用起来还是很方便,很简单的,不细说了,直接看代码吧。

说明

本例,在登录界面第二版的基础上,增加了登录界面的记住密码功能和自动登录功能。

在实现这两个功能的时候,需要用到QSettings这个知识点。QSettings用起来还是很方便,很简单的,不细说了,直接看代码吧。

保存登录信息

    # 保存登录信息
    def save_login_info(self):
        settings = QSettings("config.ini", QSettings.IniFormat)        #方法1:使用配置文件
        #settings = QSettings("mysoft","myapp")                        #方法2:使用注册表
        settings.setValue("account",self.lineEdit_account.text())
        settings.setValue("password", self.lineEdit_password.text())
        settings.setValue("remeberpassword", self.checkBox_remeberpassword.isChecked())
        settings.setValue("autologin", self.checkBox_autologin.isChecked())

初始化登录信息

    # 初始化登录信息
    def init_login_info(self):
        settings = QSettings("config.ini", QSettings.IniFormat)        #方法1:使用配置文件
        #settings = QSettings("mysoft","myapp")                        #方法2:使用注册表
        the_account =settings.value("account")
        the_password = settings.value("password")
        the_remeberpassword = settings.value("remeberpassword")
        the_autologin = settings.value("autologin")
        ########
        self.lineEdit_account.setText(the_account)
        if the_remeberpassword=="true" or  the_remeberpassword==True:
            self.checkBox_remeberpassword.setChecked(True)
            self.lineEdit_password.setText(the_password)

        if the_autologin=="true" or  the_autologin==True:
            self.checkBox_autologin.setChecked(True)

        if the_autologin == "true":   #防止注销时,自动登录
            threading.Timer(1, self.on_pushButton_enter_clicked).start()
            #self.on_pushButton_enter_clicked()

完整代码

【如下代码,完全复制,直接运行,即可使用】

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import threading
################################################
#######创建主窗口
################################################
class MainWindow(QMainWindow):
    windowList = []
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setWindowTitle('主界面')
        self.showMaximized()

        # 创建菜单栏
        self.createMenus()

    def createMenus(self):
        # 创建动作 注销
        self.printAction1 = QAction(self.tr("注销"), self)
        self.printAction1.triggered.connect(self.on_printAction1_triggered)

        # 创建动作 退出
        self.printAction2 = QAction(self.tr("退出"), self)
        self.printAction2.triggered.connect(self.on_printAction2_triggered)

        # 创建菜单,添加动作
        self.printMenu = self.menuBar().addMenu(self.tr("注销和退出"))
        self.printMenu.addAction(self.printAction1)
        self.printMenu.addAction(self.printAction2)




    # 动作一:注销
    def on_printAction1_triggered(self):
        self.close()
        dialog = logindialog(mode=1)
        if  dialog.exec_()==QDialog.Accepted:
            the_window = MainWindow()
            self.windowList.append(the_window)    #这句一定要写,不然无法重新登录
            the_window.show()



    # 动作二:退出
    def on_printAction2_triggered(self):
        self.close()



    # 关闭界面触发事件
    def closeEvent(self, event):
        print(999999999)
        pass

################################################
#######对话框
################################################
class logindialog(QDialog):
    def __init__(self,mode=0, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.mode = mode
        self.setWindowTitle('登录界面')
        self.resize(200, 200)
        self.setFixedSize(self.width(), self.height())
        self.setWindowFlags(Qt.WindowCloseButtonHint)

        ###### 设置界面控件
        self.frame = QFrame(self)
        self.verticalLayout = QVBoxLayout(self.frame)

        self.lineEdit_account = QLineEdit()
        self.lineEdit_account.setPlaceholderText("请输入账号")
        self.verticalLayout.addWidget(self.lineEdit_account)

        self.lineEdit_password = QLineEdit()
        self.lineEdit_password.setPlaceholderText("请输入密码")
        self.verticalLayout.addWidget(self.lineEdit_password)

        self.checkBox_remeberpassword = QCheckBox()
        self.checkBox_remeberpassword.setText("记住密码")
        self.verticalLayout.addWidget(self.checkBox_remeberpassword)

        self.checkBox_autologin = QCheckBox()
        self.checkBox_autologin.setText("自动登录")
        self.verticalLayout.addWidget(self.checkBox_autologin)


        self.pushButton_enter = QPushButton()
        self.pushButton_enter.setText("确定")
        self.verticalLayout.addWidget(self.pushButton_enter)

        self.pushButton_quit = QPushButton()
        self.pushButton_quit.setText("取消")
        self.verticalLayout.addWidget(self.pushButton_quit)

        ###### 绑定按钮事件
        self.pushButton_enter.clicked.connect(self.on_pushButton_enter_clicked)
        self.pushButton_quit.clicked.connect(QCoreApplication.instance().quit)


        ####初始化登录信息
        self.init_login_info()


        ####自动登录
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.goto_autologin)
        self.timer.setSingleShot(True)
        self.timer.start(1000)



    # 自动登录
    def goto_autologin(self):
        if self.checkBox_autologin.isChecked()==True and self.mode == 0 :
           self.on_pushButton_enter_clicked()




    def on_pushButton_enter_clicked(self):
        # 账号判断
        if self.lineEdit_account.text() == "":
            return

        # 密码判断
        if self.lineEdit_password.text() == "":
            return


        ####### 保存登录信息
        self.save_login_info()

        # 通过验证,关闭对话框并返回1
        self.accept()



    # 保存登录信息
    def save_login_info(self):
        settings = QSettings("config.ini", QSettings.IniFormat)        #方法1:使用配置文件
        #settings = QSettings("mysoft","myapp")                        #方法2:使用注册表
        settings.setValue("account",self.lineEdit_account.text())
        settings.setValue("password", self.lineEdit_password.text())
        settings.setValue("remeberpassword", self.checkBox_remeberpassword.isChecked())
        settings.setValue("autologin", self.checkBox_autologin.isChecked())



    # 初始化登录信息
    def init_login_info(self):
        settings = QSettings("config.ini", QSettings.IniFormat)        #方法1:使用配置文件
        #settings = QSettings("mysoft","myapp")                        #方法2:使用注册表
        the_account =settings.value("account")
        the_password = settings.value("password")
        the_remeberpassword = settings.value("remeberpassword")
        the_autologin = settings.value("autologin")
        ########
        self.lineEdit_account.setText(the_account)
        if the_remeberpassword=="true" or  the_remeberpassword==True:
            self.checkBox_remeberpassword.setChecked(True)
            self.lineEdit_password.setText(the_password)

        if the_autologin=="true" or  the_autologin==True:
            self.checkBox_autologin.setChecked(True)


################################################
#######程序入门
################################################
if __name__ == "__main__":
    app = QApplication(sys.argv)
    dialog = logindialog(mode=0)
    if  dialog.exec_()==QDialog.Accepted:
        the_window = MainWindow()
        the_window.show()
        sys.exit(app.exec_())

本文如有帮助,敬请留言鼓励。
本文如有错误,敬请留言改进。

更新备注

2019.3.6更新 优化了自动登录机制,老的自动登录机制存在BUG

目录
相关文章
|
机器学习/深度学习 人工智能 测试技术
11种开源即插即用模块汇总 !!(附论文和代码)
11种开源即插即用模块汇总 !!(附论文和代码)
1185 1
|
7月前
|
存储 弹性计算 人工智能
2026年阿里云服务器专属活动,爆款直降活动内容与云服务器活动价格参考
阿里云推出的弹性计算云服务器爆款直降活动,是云服务器ECS产品专属活动,2026年,这个活动中既有轻量应用服务器38元抢购,也有年度爆款服务器经济型e实例2核2G3M带宽 40G ESSD Entry云盘特惠价99元1年,还有通用算力型u2a实例指定配置2.5折,更强劲、更安全、更划算的第9代计算型c9i、通用型g9i、内存型r9i云服务器年付6.4折起等活动内容。本位为大家介绍爆款直降的最新活动内容,以及活动内云服务器的活动价格,以供参考和选择。
538 3
|
6月前
|
人工智能 监控 搜索推荐
阿里云万小智建站使用教程:对话式AI建站,直接说需求,10分钟网站上线!
阿里云万小智是对话式AI建站工具,用户只需口述需求,10分钟即可完成网站搭建。教程涵盖四大阶段:域名备案(1–20工作日)、AI模板设计、一键发布上线、SEO推广与流量监控,全程零代码操作,新手友好。
1134 5
|
机器学习/深度学习 人工智能 自然语言处理
大语言模型的预训练[2]:GPT、GPT2、GPT3、GPT3.5、GPT4相关理论知识和模型实现、模型应用以及各个版本之间的区别详解
大语言模型的预训练[2]:GPT、GPT2、GPT3、GPT3.5、GPT4相关理论知识和模型实现、模型应用以及各个版本之间的区别详解
大语言模型的预训练[2]:GPT、GPT2、GPT3、GPT3.5、GPT4相关理论知识和模型实现、模型应用以及各个版本之间的区别详解
|
机器人 Shell Python
ROS2教程05 ROS2服务
这篇文章是关于ROS2(Robot Operating System 2)服务的教程,涵盖了服务的概念、特性、命令行工具的使用,以及如何编写服务的服务器和客户端代码,并提供了测试服务通信机制的示例。
820 4
ROS2教程05 ROS2服务
|
存储 弹性计算 数据库
2024最新阿里云优惠券领取入口整理、代金券查询方法和使用教程
2024最新阿里云优惠券领取入口整理、代金券查询方法和使用教程。阿里云优惠券是什么?2024年阿里云优惠券领取地址链接和使用方法。阿小云连夜整理阿里云优惠券领取入口,包括领券中心、学生无门槛300元代金券、域名优惠口令、代金券查询和使用方法
2130 7
|
安全 物联网 API
API的科普
在当今这个数字化时代,信息如同血液般在无数个系统、应用和设备之间流淌,而这一切高效、无缝的交互背后,离不开一个至关重要的技术组件——API(Application Programming Interface,应用程序编程接口)。API作为数字世界的桥梁,不仅连接了不同的软件系统,还推动了数据共享、业务自动化以及创新服务的不断涌现。本文将深入探讨API的定义、作用、发展历程、关键技术、应用场景以及未来趋势,旨在揭示API在数字化转型中的核心价值和无限潜力。
2735 1
|
运维 监控 架构师
如何进行系统架构评审:全面指导与实践
【8月更文挑战第18天】系统架构评审是确保软件项目成功的关键环节之一。通过科学合理的评审流程和严格的评审要点控制,可以显著提高架构设计的质量和项目的整体成功率。
1329 3
|
算法 安全 Java
Java中MD5加密算法的原理与实现详解
Java中MD5加密算法的原理与实现详解
|
SQL 安全 算法
BugKu CTF(Crypto):Caesar cipher & 抄错的字符 & /.- & 聪明的小羊 & ok
BugKu CTF(Crypto):Caesar cipher & 抄错的字符 & /.- & 聪明的小羊 & ok