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

说明

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

在实现这两个功能的时候,需要用到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
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用PyQt5和Qt Designer实现登录界面的步骤: 1.首先,使用Qt Designer创建登录界面的UI文件。在Qt Designer中添加所需的小部件,例如标签、文本框、按钮等,并设置它们的属性和布局。保存UI文件。 2.使用PyUIC将UI文件转换为Python文件。在终端中运行以下命令: ```shell pyuic5 -o login_ui.py login.ui ``` 其中,login.ui是你保存的UI文件的名称,login_ui.py是你想要生成的Python文件的名称。 3.在Python文件中导入所需的模块和类,并编写逻辑代码。例如,你可以使用QLineEdit小部件获取用户名和密码,使用QPushButton小部件实现登录按钮,并使用QLabel小部件显示错误消息。 ```python from PyQt5 import QtWidgets, uic from PyQt5.QtWidgets import QMessageBox class LoginWindow(QtWidgets.QMainWindow): def __init__(self): super(LoginWindow, self).__init__() uic.loadUi('login_ui.py', self) self.login_button.clicked.connect(self.login) def login(self): username = self.username_input.text() password = self.password_input.text() if username == 'admin' and password == '123456': QMessageBox.information(self, 'Success', 'Login successful!') else: QMessageBox.warning(self, 'Error', 'Invalid username or password!') if __name__ == '__main__': app = QtWidgets.QApplication([]) window = LoginWindow() window.show() app.exec_() ``` 4.运行Python文件,即可看到登录界面。输入正确的用户名和密码,点击登录按钮,将显示登录成功的消息。否则,将显示错误消息。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值