在PySide2中添加登录对话框

#创建一个注册对话框(账号输入框,密码输入框,注册按钮和取消按钮)
    def reg(self):
        global qda
        qda = QDialog()
        qda.setWindowTitle("注册")
        settingLayout = QHBoxLayout()
        label = QLabel()
        label.setText("账号:")
        label.setMinimumSize(100, 0)
        settingLayout.addWidget(label)
        combox = QLineEdit()

        settingLayout.addWidget(combox)
        settingLayout1 = QHBoxLayout()
        label1 = QLabel()
        label1.setText("密码:")
        label1.setMinimumSize(100, 0)
        settingLayout1.addWidget(label1)
        combox1 = QLineEdit()
        combox1.setEchoMode(QLineEdit.Password)

        settingLayout1.addWidget(combox1)
        settingLayout2 = QHBoxLayout()
        button = QPushButton()
        button.setText("注册")
        settingLayout2.addWidget(button)
        button1 = QPushButton()
        button1.setText("取消")  
        settingLayout2.addWidget(button1)
        settingLayout3 = QVBoxLayout(qda)
        settingLayout3.addLayout(settingLayout)
        settingLayout3.addLayout(settingLayout1)
        settingLayout3.addLayout(settingLayout2)
        #当用户单击“注册”按钮时,将调用reg_ok函数来完成注册操作
        button.clicked.connect(self.reg_ok)
        #当用户单击“取消”按钮时,将调用reg_rej函数来取消注册操作
        button1.clicked.connect(self.reg_rej)
        qda.exec_()

    #注册操作(主要实现了将用户注册信息存储到 MySQL 数据库中的功能)
    def reg_ok(self):
        global qda
        #通过qda.findChildren(QLineEdit) 获取对话框中所有的 QLineEdit (输入框)对象
        childen=qda.findChildren(QLineEdit)
        #遍历 QLineEdit 对象列表,判断当前对象的 echoMode 是否为 QLineEdit.Password
        for ci in childen:
            # print(ci.echoMode)
            #如果是则将其文本赋值给变量 userN,否则将其文本赋值给变量 useP
            if ci.echoMode()==QLineEdit.Password:
                userN=ci.text()
            else:
                useP=ci.text()
        #使用 pymysql 模块连接 MySQL 数据库
        db = pymysql.connect(host='localhost',
                     port=3306,
                     user='root',
                     passwd='123456',
                     database='db',
                     charset='utf8')
        #创建游标对象cursor_acc
        cursor= db.cursor()
        #创建数据表acc
        sql = "create table if not exists acc (name varchar(500),pass varchar(100))"
        cursor.execute(sql)
        #使用 userinfo 插入语句将用户名和密码插入到 acc 表中,其中 %s 表示占位符,会在 execute 函数的第二个参数中被具体的值替代
        userinfo= ("INSERT INTO acc(name,pass)" "VALUES(%s,%s)")
        cursor.execute(userinfo,(userN,useP))
        db.commit()
        cursor.close()
        db.close()
        qda.close()

    #点击取消,reg_rej() 方法被调用,退出注册操作
    def reg_rej(self):
        global qda
        qda.close()

        调用该对话框的方式

self.ui.bt_reg.clicked.connect(self.reg)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

k54kdk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值