手把手教你开发Python桌面应用-PyQt6图书管理系统-修改密码代码逻辑实现

锋哥原创的PyQt6图书管理系统视频教程:

PyQt6图书管理系统视频教程 Python桌面开发 Python入门级项目实战 (无废话版) 火爆连载更新中~_哔哩哔哩_bilibiliPyQt6图书管理系统视频教程 Python桌面开发 Python入门级项目实战 (无废话版) 火爆连载更新中~共计24条视频,包括:PyQt6图书管理系统视频教程 Python桌面开发 Python入门级项目实战 (无废话版) 火爆连载更新中~、第2讲 登录功能UI设计实现、第3讲 数据库操作工具包dbUtil.py封装等,UP主更多精彩视频,请关注UP账号。icon-default.png?t=N7T8https://www.bilibili.com/video/BV18t4y1R7Qp/前面我们发现UI密码框忘记设置密码显示了。三个密码输入框加下EchoMode设置

setEchoMode(QtWidgets.QLineEdit.EchoMode.Password)

首先我们先实现下 显示当前用户的用户名

前面我们在userDao.py记录了当前用户对象

# 设置用户名
        self.userNameLabel.setText(userDao.currentUser.userName)

运行测试:

因为传递给dao层方法的user对象参数会有一个新密码,所以实体里面,也加下新密码字段,以及一个重载构造方法。

"""
    用户实体类
    作者 : 小锋老师
    官网 : www.python222.com
"""


# 用户实体类
class User:
    # 编号 主键ID
    id = None
    # 用户名
    userName = None
    # 密码
    password = None
    # 新密码
    newPassword = None

    def __init__(self, userName, password):
        self.userName = userName
        self.password = password

    @staticmethod
    def my_constructor(userName, password, newPassword):
        obj = User(userName, password)
        obj.newPassword = newPassword
        return obj

userDao.py里编写下修改密码方法:

def modifyPassword(user: User):
    """
    修改密码
    :param user: 用户实体
    :return: 返回执行的记录条数
    """
    con = None
    try:
        con = dbUtil.getCon()
        cursor = con.cursor()
        cursor.execute(
            f"update t_user set password='{user.newPassword}' where userName='{user.userName}'")
        return cursor.rowcount
    except Exception as e:
        print(e)
        con.rollback()
        return 0
    finally:
        dbUtil.closeCon(con)

modifyPassword.py的Ui_Form类里加下modifyPwd方法

    def modifyPwd(self):
        userName = self.userNameLabel.text()
        oldPwd = self.oldPwdInput.text()
        if oldPwd.strip() == "":
            QMessageBox.warning(None, '系统提示', '请输入原密码!')
            return
        # 判断原密码
        if oldPwd != userDao.currentUser.password:
            QMessageBox.warning(None, '系统提示', '原密码输入不正确!')
            return
        newPwd = self.newPwdInput.text()
        if newPwd.strip() == "":
            QMessageBox.warning(None, '系统提示', '请输入新密码!')
            return
        newPwd2 = self.newPwdInput2.text()
        if newPwd2.strip() == "":
            QMessageBox.warning(None, '系统提示', '请输入确认新密码!')
            return
        # 判断新密码和确认新密码是否一致
        if newPwd != newPwd2:
            QMessageBox.warning(None, '系统提示', '确认新密码输入不正确!')
            return
        user = User.my_constructor(userName, oldPwd, newPwd)
        if userDao.modifyPassword(user) > 0:
            QMessageBox.information(None, '系统提示', '密码修改成功!')
            self.resetForm()
        else:
            QMessageBox.warning(None, '系统提示', '密码修改失败!')

    def resetForm(self):
        self.oldPwdInput.setText("")
        self.newPwdInput.setText("")
        self.newPwdInput2.setText("")

提交按钮绑定下事件

# 提交按钮点击事件绑定
        self.submitBtn.clicked.connect(self.modifyPwd)

最后运行测试:

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值