在学生主页,主要实现以下功能:
1、借阅书籍
2、归还书籍
3、借阅状态查询
4、书籍库存查询
使用四个QPushButton
,分别用于弹出借阅书籍对话框、归还书籍对话框 、切换到借阅状态查询、切换到书籍库存查询
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon, QFont
from PyQt5.QtCore import Qt
import sip
import qdarkstyle
from BookStorageViewer import BookStorageViewer
from borrowBookDialog import borrowBookDialog
from returnBookDialog import returnBookDialog
from BorrowStatusViewer import BorrowStatusViewer
class StudentHome(QWidget):
def __init__(self, studentId):
super().__init__()
self.StudentId = studentId
self.resize(900, 600)
self.setWindowTitle("欢迎使用图书馆管理系统")
self.setUpUI()
def setUpUI(self):
# 总布局
self.layout = QHBoxLayout(self)
# 按钮布局
self.buttonLayout = QVBoxLayout()
# 按钮
self.borrowBookButton = QPushButton("借书")
self.returnBookButton = QPushButton("还书")
self.myBookStatus = QPushButton("借阅状态")
self.allBookButton = QPushButton("所有书籍")
self.buttonLayout.addWidget(self.borrowBookButton)
self.buttonLayout.addWidget(self.returnBookButton)
self.buttonLayout.addWidget(self.myBookStatus)
self.buttonLayout.addWidget(self.allBookButton)
self.borrowBookButton.setFixedWidth(100)
self.borrowBookButton.setFixedHeight(42)
self.returnBookButton.setFixedWidth(100)
self.returnBookButton.setFixedHeight(42)
self.myBookStatus.setFixedWidth(100)
self.myBookStatus.setFixedHeight(42)
self.allBookButton.setFixedWidth(100)
self.allBookButton.setFixedHeight(42)
font = QFont()
font.setPixelSize(16)
self.borrowBookButton.setFont(font)
self.returnBookButton.setFont(font)
self.myBookStatus.setFont(font)
self.allBookButton.setFont(font)
self.storageView = BookStorageViewer()
self.borrowStatusView=BorrowStatusViewer(self.StudentId)
self.allBookButton.setEnabled(False)
self.layout.addLayout(self.buttonLayout)
self.layout.addWidget(self.storageView)
self.borrowBookButton.clicked.connect(self.borrowBookButtonClicked)
self.returnBookButton.clicked.connect(self.returnBookButtonClicked)
self.myBookStatus.clicked.connect(self.myBookStatusClicked)
self.allBookButton.clicked.connect(self.allBookButtonClicked)
def borrowBookButtonClicked(self):
borrowDialog = borrowBookDialog(self.StudentId,self)
borrowDialog.borrow_book_success_signal.connect(self.borrowStatusView.borrowedQuery)
borrowDialog.borrow_book_success_signal.connect(self.storageView.searchButtonClicked)
borrowDialog.show()
borrowDialog.exec_()
return
def returnBookButtonClicked(self):
returnDialog = returnBookDialog(self.StudentId,self)
returnDialog.return_book_success_signal.connect(self.borrowStatusView.returnedQuery)
returnDialog.return_book_success_signal.connect(self.borrowStatusView.borrowedQuery)
returnDialog.return_book_success_signal.connect(self.storageView.searchButtonClicked)
returnDialog.show()
returnDialog.exec_()
def myBookStatusClicked(self):
self.layout.removeWidget(self.storageView)
sip.delete(self.storageView)
self.storageView = BookStorageViewer()
self.borrowStatusView = BorrowStatusViewer(self.StudentId)
self.layout.addWidget(self.borrowStatusView)
self.allBookButton.setEnabled(True)
self.myBookStatus.setEnabled(False)
return
def allBookButtonClicked(self):
self.layout.removeWidget(self.borrowStatusView)
sip.delete(self.borrowStatusView)
self.borrowStatusView = BorrowStatusViewer(self.StudentId)
self.storageView = BookStorageViewer()
self.layout.addWidget(self.storageView)
self.allBookButton.setEnabled(False)
self.myBookStatus.setEnabled(True)
return
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setWindowIcon(QIcon("./images/MainWindow_1.png"))
app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
mainMindow = StudentHome("PB15000135")
mainMindow.show()
sys.exit(app.exec_())
修改密码功能使用菜单栏
QAction
点击启动
修改密码,主要需要用户输入学号, 原密码,新密码,确认密码实现
代码逻辑如下:
- 确认学号与原密码对应,否则报错
- 输入新密码与确认密码 ,如果相同,则md5加密后放入数据库中
- 注意设置输入校验
使用对话框实现
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import qdarkstyle
import time
from PyQt5.QtSql import *
import hashlib
class changePasswordDialog(QDialog):
def __init__(self, parent=None):
super(changePasswordDialog, self).__init__(parent)
self.setWindowModality(Qt.WindowModal)
self.setWindowTitle("修改密码")
self.setUpUI()
def setUpUI(self):
self.resize(300, 280)
self.layout = QFormLayout()
self.setLayout(self.layout)
self.titlelabel = QLabel(" 修改密码")
self.studentIdLabel = QLabel("学 号:")
# self.studentNameLabel=QLabel("姓 名:")
self.oldPasswordLabel = QLabel("旧 密 码:")
self.passwordLabel = QLabel("新 密 码:")
self.confirmPasswordLabel = QLabel("确认密码:")
self.studentIdEdit = QLineEdit()
# self.studentNameEdit=QLineEdit()
self.oldPasswordEdit = QLineEdit()
self.passwordEdit = QLineEdit()
self.confirmPasswordEdit = QLineEdit()
self.changePasswordButton = QPushButton("确认修改")
self.changePasswordButton.setFixedWidth(140)
self.changePasswordButton.setFixedHeight(32)
self.layout.addRow("", self.titlelabel)
self.layout.addRow(self.studentIdLabel, self.studentIdEdit)
# self.layout.addRow(self.studentNameLabel,self.studentNameEdit)
self.layout.addRow(self.oldPasswordLabel, self.oldPasswordEdit)
self.layout.addRow(self.passwordLabel, self.passwordEdit)
self.layout.addRow(self.confirmPasswordLabel, self.confirmPasswordEdit)
self.layout.addRow("", self.changePasswordButton)
font = QFont()
font.setPixelSize(20)
self.titlelabel.setFont(font)
font.setPixelSize(16)
self.studentIdLabel.setFont(font)
# self.studentNameLabel.setFont(font)
self.oldPasswordLabel.setFont(font)
self.passwordLabel.setFont(font)
self.confirmPasswordLabel.setFont(font)
font.setPixelSize(16)
self.studentIdEdit.setFont(font)
self.changePasswordButton.setFont(font)
# self.studentNameEdit.setFont(font)
font.setPixelSize(10)
self.oldPasswordEdit.setFont(font)
self.passwordEdit.setFont(font)
self.confirmPasswordEdit.setFont(font)
self.titlelabel.setMargin(8)
self.layout.setVerticalSpacing(10)
# 设置长度
self.studentIdEdit.setMaxLength(10)
self.oldPasswordEdit.setMaxLength(16)
self.passwordEdit.setMaxLength(16)
self.confirmPasswordEdit.setMaxLength(16)
# 设置密码掩膜
self.oldPasswordEdit.setEchoMode(QLineEdit.Password)
self.passwordEdit.setEchoMode(QLineEdit.Password)
self.confirmPasswordEdit.setEchoMode(QLineEdit.Password)
# 设置校验
reg = QRegExp("PB[0~9]{8}")
pValidator = QRegExpValidator(self)
pValidator.setRegExp(reg)
self.studentIdEdit.setValidator(pValidator)
reg = QRegExp("[a-zA-z0-9]+$")
pValidator.setRegExp(reg)
self.oldPasswordEdit.setValidator(pValidator)
self.passwordEdit.setValidator(pValidator)
self.confirmPasswordEdit.setValidator(pValidator)
# 设置信号与槽
self.changePasswordButton.clicked.connect(self.changePasswordButtonClicked)
def changePasswordButtonClicked(self):
studentId = self.studentIdEdit.text()
oldPassword = self.oldPasswordEdit.text()
password = self.passwordEdit.text()
confirmPassword = self.confirmPasswordEdit.text()
if (studentId == "" or oldPassword == "" or password == "" or confirmPassword == ""):
print(QMessageBox.warning(self, "警告", "输入不可为空,请重新输入", QMessageBox.Yes, QMessageBox.Yes))
return
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName('./db/LibraryManagement.db')
db.open()
query = QSqlQuery()
sql = "SELECT * FROM User WHERE StudentId='%s'" % studentId
query.exec_(sql)
# 如果用户不存在
if (not query.next()):
print(QMessageBox.warning(self, "警告", "该用户不存在,请重新输入", QMessageBox.Yes, QMessageBox.Yes))
self.studentIdEdit.clear()
return
# 如果密码错误
hl = hashlib.md5()
hl.update(oldPassword.encode(encoding='utf-8'))
md5password = hl.hexdigest()
sql = "SELECT * FROM User WHERE Password='%s' AND StudentId='%s'" %(md5password,studentId)
query.exec_(sql)
if (not query.next()):
print(QMessageBox.warning(self, "警告", "原密码输入错误,请重新输入", QMessageBox.Yes, QMessageBox.Yes))
self.oldPasswordEdit.clear()
return
# 密码与确认密码不同
if(password!=confirmPassword):
print(QMessageBox.warning(self,"警告","两次输入密码不同,请确认输入",QMessageBox.Yes,QMessageBox.Yes))
self.passwordEdit.clear()
self.confirmPasswordEdit.clear()
return
# 修改密码
hl = hashlib.md5()
hl.update(password.encode(encoding='utf-8'))
md5password = hl.hexdigest()
sql="UPDATE User SET Password='%s' WHERE StudentId='%s'"%(md5password,studentId)
query.exec_(sql)
db.commit()
QMessageBox.information(self,"提醒","修改密码成功,请登录系统!",QMessageBox.Yes,QMessageBox.Yes)
self.close()
return
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setWindowIcon(QIcon("./images/MainWindow_1.png"))
app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
mainMindow = changePasswordDialog()
mainMindow.show()
sys.exit(app.exec_())
项目地址:github仓库链接