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

目录
相关文章
|
8月前
|
Java 测试技术 定位技术
《手把手教你》系列技巧篇(二十三)-java+ selenium自动化测试-webdriver处理浏览器多窗口切换下卷(详细教程)
【4月更文挑战第15天】本文介绍了如何使用Selenium进行浏览器窗口切换以操作不同页面元素。首先,获取浏览器窗口句柄有两种方法:获取所有窗口句柄的集合和获取当前窗口句柄。然后,通过`switchTo().window()`方法切换到目标窗口句柄。在项目实战部分,给出了一个示例,展示了在百度首页、新闻页面和地图页面之间切换并输入文字的操作。最后,文章还探讨了在某些情况下可能出现的问题,并提供了一个简单的本地HTML页面示例来演示窗口切换的正确操作。
148 0
|
8月前
|
Java 测试技术 定位技术
《手把手教你》系列技巧篇(二十一)-java+ selenium自动化测试-浏览器窗口的句柄(详细教程)
【4月更文挑战第13天】本文介绍了如何获取浏览器窗口句柄,句柄是标识浏览器窗口的唯一ID。文章首先解释了窗口句柄的概念,然后通过Java代码示例展示了在单个、多个窗口句柄情况下的操作,包括打印单个窗口句柄和获取所有窗口句柄的方法。在多窗口句柄的场景中,代码演示了如何在不同标签页之间切换。最后,文章强调了句柄在实际操作中的重要性,特别是在处理多个窗口时。
123 0
|
8月前
|
Java 测试技术 Python
《手把手教你》系列技巧篇(二十二)-java+ selenium自动化测试-webdriver处理浏览器多窗口切换上卷(详细教程)
【4月更文挑战第14天】本文介绍了在Web自动化测试中如何使用Selenium进行浏览器窗口的切换。首先,获取浏览器窗口句柄有两种方式:获取所有窗口句柄的集合和获取当前窗口句柄。然后,通过`switchTo().window()`方法切换到目标窗口。在项目实战部分,展示了如何在京东网站上实现页面间的切换,包括点击手机链接打开新窗口,然后切换到新窗口并点击小米链接。文章还提供了两种不同的代码实现方式,并给出了运行代码后的控制台输出和浏览器动作演示。最后,作者建议将窗口切换的逻辑封装成方法以提高代码复用性。
132 0
|
8月前
|
API Python
基于Python PYQT5的GUI亚丁号辅助登陆界面
基于Python PYQT5的GUI亚丁号辅助登陆界面
56 2
|
8月前
|
数据可视化 C# 图形学
【Unity 3D】图形界面GUI的讲解及在C#中实现用户登录界面的实战(附源码)
【Unity 3D】图形界面GUI的讲解及在C#中实现用户登录界面的实战(附源码)
260 0
|
C++ Python
Python密码锁屏窗体界面
Python密码锁屏窗体界面
157 0
Python密码锁屏窗体界面
|
前端开发 Linux Python
使用tkinter创建登录界面
使用tkinter创建登录界面
90 0
|
Python
Tkinter模块GUI界面化编程实战(一)——登录界面(含详解及完整源码、完整程序下载链接)
Tkinter模块GUI界面化编程实战(一)——登录界面(含详解及完整源码、完整程序下载链接)
336 0
|
数据安全/隐私保护 C语言 Python
Python高级进阶教程021期 pyqt5label控件进阶使用,设置兄弟控件,广告植入
Python高级进阶教程021期 pyqt5label控件进阶使用,设置兄弟控件,广告植入
121 0
【Pyqt5】实现登录界面、主界面的相互跳转
【Pyqt5】实现登录界面、主界面的相互跳转