#创建一个注册对话框(账号输入框,密码输入框,注册按钮和取消按钮)
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)