环境配置
- pip install pyqt5
- pip install pyqt5-tools(/官网)
- pyqt5-tools 下载地址:Links for pyqt5-tools (pypi.org) ---pyqt-tools的安装包(.whl)下载网址
镜像源:
- 阿里云 http://mirrors.aliyun.com/pypi/simple/
- 豆瓣http://pypi.douban.com/simple/
- 清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
- 中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
- 华中科技大学http://pypi.hustunique.com/
一、类设置
class SignUpWidet(QWidget):
student_signup_signal = pyqtSignal(str)
def __init__(self):
super().__init__()
self.setUpUI()
def setUpUI(self):
二、创建窗口部件
(一)设置页面布局
def setUpUI(self):
self.resize(800,600)
self.setWindowTitle("欢迎登录图书管理系统")
# 文本
self.signUPLabel = QLabel("注 册")
# 把QLabel居中
self.signUPLabel.setAlignment(Qt.AlignCenter)
# 设置位置
self.signUPLabel.setFixedHeight(100)
# 设置字体
font = QFont()
font.setPixelSize(36)
lineEditFont = QFont()
lineEditFont.setPixelSize(16)
self.signUPLabel.setFont(font)
# 垂直布局
self.layout = QVBoxLayout()
self.layout.addWidget(self.signUPLabel, Qt.AlignCenter)
self.setLayout(self.layout)
# 表单:包括学号、姓名、密码、确认密码
self.formLayout = QFormLayout()
font.setPixelSize(18)
# rows 行 columns 列
# rows1
self.studentIDLable = QLabel("学 号:")
# 设置学号字体
self.studentIDLable.setFont(font)
# 输入框
self.studentIDLineEdit = QLineEdit()
# 设置输入框的宽度
self.studentIDLineEdit.setFixedWidth(180)
# 设置输入框的高度
self.studentIDLineEdit.setFixedHeight(32)
# 设置输入框的字体
self.studentIDLineEdit.setFont(lineEditFont)
# 限制输入字数
self.studentIDLineEdit.setMaxLength(10)
# 添加字体标签和输入框标签
self.formLayout.addRow(self.studentIDLable,self.studentIDLineEdit)
# rows2
self.studentNameLable = QLabel("姓 名:")
# 设置学号字体
self.studentNameLable.setFont(font)
# 输入框
self.studentNameLineEdit = QLineEdit()
# 设置输入框的宽度
self.studentNameLineEdit.setFixedWidth(180)
# 设置输入框的高度
self.studentNameLineEdit.setFixedHeight(32)
# 设置输入框的字体
self.studentNameLineEdit.setFont(lineEditFont)
# 限制输入字数
self.studentNameLineEdit.setMaxLength(10)
# 添加字体标签和输入框标签
self.formLayout.addRow(self.studentNameLable,self.studentNameLineEdit)
# rows3
self.passwordLable = QLabel("密 码:")
# 设置学号字体
self.passwordLable.setFont(font)
# 输入框
self.passwordLineEdit = QLineEdit()
# 设置输入框的宽度
self.passwordLineEdit.setFixedWidth(180)
# 设置输入框的高度
self.passwordLineEdit.setFixedHeight(32)
# 设置输入框的字体
self.passwordLineEdit.setFont(lineEditFont)
# 限制输入字数
self.passwordLineEdit.setMaxLength(10)
# 添加字体标签和输入框标签
self.formLayout.addRow(self.passwordLable,self.passwordLineEdit)
# rows4
self.passwordConfirmLable = QLabel("确 认:")
# 设置学号字体
self.passwordConfirmLable.setFont(font)
# 输入框
self.passwordConfirmLineEdit = QLineEdit()
# 设置输入框的宽度
self.passwordConfirmLineEdit.setFixedWidth(180)
# 设置输入框的高度
self.passwordConfirmLineEdit.setFixedHeight(32)
# 设置输入框的字体
self.passwordConfirmLineEdit.setFont(lineEditFont)
# 限制输入字数
self.passwordConfirmLineEdit.setMaxLength(10)
# 添加字体标签和输入框标签
self.formLayout.addRow(self.passwordConfirmLable,self.passwordConfirmLineEdit)
# row5
self.signUPbutton = QPushButton("注册")
self.signUPbutton.setFixedWidth(120)
self.signUPbutton.setFixedHeight(30)
self.signUPbutton.setFont(font)
self.formLayout.addRow("",self.signUPbutton)
(二)设置表单布局
# 调整表单布局
widget = QWidget()
widget.setLayout(self.formLayout)
widget.setFixedWidth(300)
widget.setFixedHeight(250)
# 从左往右布局
self.Hlayout = QHBoxLayout()
# 居中
self.Hlayout.addWidget(widget,Qt.AlignCenter)
widget = QWidget()
widget.setLayout(self.Hlayout)
self.layout.addWidget(widget,Qt.AlignHCenter)
三、注册函数
- 接上
def setUpUI(self):
(一)设置验证,进行匹配密码
# 用正则区分 PB[0~9]匹配0~9的数字,{8}只能匹配8个
reg = QRegExp("PB[0~9]{8}")
(二)调用并绑定
1、调用
# 指向调用对象 QRegExpValidator 根据正则表达式检测字符串
pValidator = QRegExpValidator(self)
# 设置调用
pValidator.setRegExp(reg)
2、绑定
# 绑定
self.studentIDLineEdit.setValidator(pValidator)
reg = QRegExp("[a-zA-Z0-9]+$")
pValidator.setRegExp(reg)
self.passwordLineEdit.setValidator(pValidator)
(三)设置连接触发事件(发出信号)
# 设置连接,触发事件
self.signUPbutton.clicked.connect(self.SignUp)
# 发出信号
self.studentIDLineEdit.returnPressed.connect(self.SignUp)
self.studentNameLineEdit.returnPressed.connect(self.SignUp)
self.passwordLineEdit.returnPressed.connect(self.SignUp)
self.passwordConfirmLineEdit.returnPressed.connect(self.SignUp)
(四)接受信号
- 调用新函数,接受触发的事件进行处理。
1、接受输入框内容
def SignUp(self):
# 触发事件后用于接受输入框内字符串
studentID = self.studentIDLineEdit.text()
# print(studentID)
studentName = self.studentNameLineEdit.text()
password = self.passwordLineEdit.text()
confirmPassword = self.passwordConfirmLineEdit.text()
2、逻辑判定输入框的事件
- 弹出消息提示框
if (studentID == "" or studentName == "" or password == "" or confirmPassword == ""):
print(QMessageBox.warning(self,"警告","输入内容不能为空。",QMessageBox.Yes,QMessageBox.Yes))
return
(五)逻辑处理
1、打开数据库
else:
# 进行逻辑处理
# 打开指定的数据库
db = QSqlDatabase.addDatabase("QSQLITE") # QSQLITE sqlite专有的数据库
db.setDatabaseName("./db/LibraryManagement.db")
db.open()
query = QSqlQuery() # Q版的sql语句查询函数
2、逻辑判断
(1)若输入框为空进行消息框提醒,若不为空执行以下语句。
if (studentID == "" or studentName == "" or password == "" or confirmPassword == ""):
print(QMessageBox.warning(self,"警告","输入内容不能为空。",QMessageBox.Yes,QMessageBox.Yes))
return
(2)判断两次密码是否相等:
①不相等,提醒,返回函数
if (confirmPassword != password):
print(QMessageBox.warning(self, "警告","两次密码输入的不一致,请重新输入",QMessageBox.Yes,QMessageBox.Yes))
return
②相等,密码加密存入数据库,判断账号是否存在:
elif (confirmPassword == password):
# md5编码
h1 = hashlib.md5()
h1.update(password.encode(encoding="utf-8"))
md5password = h1.hexdigest()
sql = "select * from user where StudentId='%s'"%(studentID)
query.exec_(sql)
if (query.next()):
存在:提醒,返回函数
if (query.next()):
print(QMessageBox.warning(self,"警告","该账号已经存在,请重新输入",QMessageBox.Yes,QMessageBox.Yes))
return
不存在:存入数据库,注册成功提醒!
else:
sql = "insert into user values ('%s', '%s', '%s', 0,0,0)"%(studentID,studentName,md5password)
db.exec_(sql)
db.commit()
print(QMessageBox.information(self,"提醒","您已成功注册账号",QMessageBox.Yes,QMessageBox.Yes))
self.student_signup_signal.emit(studentID)
db.close()
return
四、程序入口运行
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
m = SignUpWidet()
m.show()
sys.exit(app.exec_())
五、完整代码
import sys
from PyQt5.QtWidgets import *
import qdarkstyle
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtSql import *
import hashlib
from PyQt5.QtWidgets import QMessageBox
class SignUpWidet(QWidget):
student_signup_signal = pyqtSignal(str)
def __init__(self):
super().__init__()
self.setUpUI()
def setUpUI(self):
self.resize(800,600)
self.setWindowTitle("欢迎登录图书管理系统")
# 文本
self.signUPLabel = QLabel("注 册")
# 把QLabel居中
self.signUPLabel.setAlignment(Qt.AlignCenter)
# 设置位置
self.signUPLabel.setFixedHeight(100)
# 设置字体
font = QFont()
font.setPixelSize(36)
lineEditFont = QFont()
lineEditFont.setPixelSize(16)
self.signUPLabel.setFont(font)
# 垂直布局
self.layout = QVBoxLayout()
self.layout.addWidget(self.signUPLabel, Qt.AlignCenter)
self.setLayout(self.layout)
# 表单:包括学号、姓名、密码、确认密码
self.formLayout = QFormLayout()
font.setPixelSize(18)
# rows 行 columns 列
# rows1
self.studentIDLable = QLabel("学 号:")
# 设置学号字体
self.studentIDLable.setFont(font)
# 输入框
self.studentIDLineEdit = QLineEdit()
# 设置输入框的宽度
self.studentIDLineEdit.setFixedWidth(180)
# 设置输入框的高度
self.studentIDLineEdit.setFixedHeight(32)
# 设置输入框的字体
self.studentIDLineEdit.setFont(lineEditFont)
# 限制输入字数
self.studentIDLineEdit.setMaxLength(10)
# 添加字体标签和输入框标签
self.formLayout.addRow(self.studentIDLable,self.studentIDLineEdit)
# rows2
self.studentNameLable = QLabel("姓 名:")
# 设置学号字体
self.studentNameLable.setFont(font)
# 输入框
self.studentNameLineEdit = QLineEdit()
# 设置输入框的宽度
self.studentNameLineEdit.setFixedWidth(180)
# 设置输入框的高度
self.studentNameLineEdit.setFixedHeight(32)
# 设置输入框的字体
self.studentNameLineEdit.setFont(lineEditFont)
# 限制输入字数
self.studentNameLineEdit.setMaxLength(10)
# 添加字体标签和输入框标签
self.formLayout.addRow(self.studentNameLable,self.studentNameLineEdit)
# rows3
self.passwordLable = QLabel("密 码:")
# 设置学号字体
self.passwordLable.setFont(font)
# 输入框
self.passwordLineEdit = QLineEdit()
# 设置输入框的宽度
self.passwordLineEdit.setFixedWidth(180)
# 设置输入框的高度
self.passwordLineEdit.setFixedHeight(32)
# 设置输入框的字体
self.passwordLineEdit.setFont(lineEditFont)
# 限制输入字数
self.passwordLineEdit.setMaxLength(10)
# 添加字体标签和输入框标签
self.formLayout.addRow(self.passwordLable,self.passwordLineEdit)
# rows4
self.passwordConfirmLable = QLabel("确 认:")
# 设置学号字体
self.passwordConfirmLable.setFont(font)
# 输入框
self.passwordConfirmLineEdit = QLineEdit()
# 设置输入框的宽度
self.passwordConfirmLineEdit.setFixedWidth(180)
# 设置输入框的高度
self.passwordConfirmLineEdit.setFixedHeight(32)
# 设置输入框的字体
self.passwordConfirmLineEdit.setFont(lineEditFont)
# 限制输入字数
self.passwordConfirmLineEdit.setMaxLength(10)
# 添加字体标签和输入框标签
self.formLayout.addRow(self.passwordConfirmLable,self.passwordConfirmLineEdit)
# row5
self.signUPbutton = QPushButton("注册")
self.signUPbutton.setFixedWidth(120)
self.signUPbutton.setFixedHeight(30)
self.signUPbutton.setFont(font)
self.formLayout.addRow("",self.signUPbutton)
# 调整表单布局
widget = QWidget()
widget.setLayout(self.formLayout)
widget.setFixedWidth(300)
widget.setFixedHeight(250)
# 从左往右布局
self.Hlayout = QHBoxLayout()
# 居中
self.Hlayout.addWidget(widget,Qt.AlignCenter)
widget = QWidget()
widget.setLayout(self.Hlayout)
self.layout.addWidget(widget,Qt.AlignHCenter)
# 设置验证,密码匹配
# 用正则传 PB[0~9]匹配0~9的数字,{8}只能匹配8个
reg = QRegExp("PB[0~9]{8}")
# 指向调用对象 QRegExpValidator 根据正则表达式检测字符串
pValidator = QRegExpValidator(self)
# 设置调用
pValidator.setRegExp(reg)
# 绑定
self.studentIDLineEdit.setValidator(pValidator)
reg = QRegExp("[a-zA-Z0-9]+$")
pValidator.setRegExp(reg)
self.passwordLineEdit.setValidator(pValidator)
# 设置连接,触发事件
self.signUPbutton.clicked.connect(self.SignUp)
# 发出信号
self.studentIDLineEdit.returnPressed.connect(self.SignUp)
self.studentNameLineEdit.returnPressed.connect(self.SignUp)
self.passwordLineEdit.returnPressed.connect(self.SignUp)
self.passwordConfirmLineEdit.returnPressed.connect(self.SignUp)
def SignUp(self):
# 触发事件后用于接受输入框内字符串
studentID = self.studentIDLineEdit.text()
# print(studentID)
studentName = self.studentNameLineEdit.text()
password = self.passwordLineEdit.text()
confirmPassword = self.passwordConfirmLineEdit.text()
if (studentID == "" or studentName == "" or password == "" or confirmPassword == ""):
print(QMessageBox.warning(self,"警告","输入内容不能为空。",QMessageBox.Yes,QMessageBox.Yes))
return
else:
# 进行逻辑处理
# 打开指定的数据库
db = QSqlDatabase.addDatabase("QSQLITE") # QSQLITE sqlite专有的数据库
db.setDatabaseName("./db/LibraryManagement.db")
db.open()
query = QSqlQuery() # Q版的sql语句查询函数
if (confirmPassword != password):
print(QMessageBox.warning(self, "警告","两次密码输入的不一致,请重新输入",QMessageBox.Yes,QMessageBox.Yes))
return
elif (confirmPassword == password):
# md5编码
h1 = hashlib.md5()
h1.update(password.encode(encoding="utf-8"))
md5password = h1.hexdigest()
sql = "select * from user where StudentId='%s'"%(studentID)
query.exec_(sql)
if (query.next()):
print(QMessageBox.warning(self,"警告","该账号已经存在,请重新输入",QMessageBox.Yes,QMessageBox.Yes))
return
else:
sql = "insert into user values ('%s', '%s', '%s', 0,0,0)"%(studentID,studentName,md5password)
db.exec_(sql)
db.commit()
print(QMessageBox.information(self,"提醒","您已成功注册账号",QMessageBox.Yes,QMessageBox.Yes))
self.student_signup_signal.emit(studentID)
db.close()
return
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
m = SignUpWidet()
m.show()
sys.exit(app.exec_())
待后续...
标签:设置,--,self,pyqt5,输入框,字体,QMessageBox,setFont,页面 From: https://www.cnblogs.com/LoLong/p/16951179.html