pyqt5图书管理系统--8、学生页面设计、借阅状态页面设计和所有书籍页面设计
本节分为三个部分:学生页面、借阅状态页面和所有书籍查询页面。
主要流程:1、学生用户主界面,可以使用借阅书籍、归还书籍、查看借阅状态、查看所有书籍信息。
2、学生借阅状态页面,可以查看已经借阅的书籍,和已归还的书籍信息。
3、整合所有学生页面的模块,进入学生页面后显示所有书籍查询界面及其他功能。
一、借阅状态页面
导入的模块:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt
import qdarkstyle
from PyQt5.QtSql import *
(一)类基础设置
- 设置页面大小,调用主函数self.setUpUI()
class BorrowStatusViewer(QWidget):
def __init__(self, StudentId):
super(BorrowStatusViewer, self).__init__()
self.resize(700, 500)
self.studentId = StudentId
self.setWindowTitle("欢迎使用图书管理系统")
self.setUpUI()
(二)借阅状态页面布局
1、连接数据库
def setUpUI(self):
self.db = QSqlDatabase.addDatabase("QSQLITE")
self.db.setDatabaseName('./db/LibraryManagement.db')
self.db.open()
# 分为两块,上方是已借未归还,下方是已归还
self.layout = QVBoxLayout(self)
2、设置未归还和已归还标签及大小
self.borrowedLabel = QLabel("未归还:")
self.returnedLabel = QLabel("已归还:")
self.borrowedLabel.setFixedHeight(32)
self.borrowedLabel.setFixedWidth(60)
self.returnedLabel.setFixedHeight(32)
self.returnedLabel.setFixedWidth(60)
font = QFont()
font.setPixelSize(18)
self.borrowedLabel.setFont(font)
self.returnedLabel.setFont(font)
3、设置未归还表格
- 表格模型QTableView()
self.borrowedTableView = QTableView()
# 设置QT表格的宽度自适应 horizontalHeader() 宽度自适应内容宽度以及填充整个表格
self.borrowedTableView.horizontalHeader().setStretchLastSection(True)
# 自动填充
self.borrowedTableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
# 表格内容不能修改
self.borrowedTableView.setEditTriggers(QAbstractItemView.NoEditTriggers)
- 在表格模型内设置数据库查询模型
- 数据库查询模型QSqlQueryModel()
self.borrowedQueryModel = QSqlQueryModel()
self.borrowedTableView.setModel(self.borrowedQueryModel)
- 调用未归还书籍查询函数
-
self.borrowedQuery()
def borrowedQuery(self):
sql = "SELECT Book.BookName,Book.BookId,Auth,Category,Publisher,PublishTime,BorrowTime FROM Book,User_Book WHERE Book.BookId=User_Book.BookId AND User_Book.BorrowState=1 AND StudentId='%s'" % self.studentId
self.borrowedQueryModel.setQuery(sql)
return
- 设置列标题
self.borrowedQueryModel.setHeaderData(0, Qt.Horizontal, "书名")
self.borrowedQueryModel.setHeaderData(1, Qt.Horizontal, "书号")
self.borrowedQueryModel.setHeaderData(2, Qt.Horizontal, "作者")
self.borrowedQueryModel.setHeaderData(3, Qt.Horizontal, "分类")
self.borrowedQueryModel.setHeaderData(4, Qt.Horizontal, "出版社")
self.borrowedQueryModel.setHeaderData(5, Qt.Horizontal, "出版时间")
self.borrowedQueryModel.setHeaderData(6, Qt.Horizontal, "借出时间")
- 未归还页面效果
4、设置已归还表格
- 表格模型QTableView()
self.returnedTableView = QTableView()
self.returnedTableView.horizontalHeader().setStretchLastSection(True)
self.returnedTableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.returnedTableView.setEditTriggers(QAbstractItemView.NoEditTriggers)
- 在表格模型内设置数据库查询模型
- 数据库查询模型QSqlQueryModel()
self.returnedQueryModel = QSqlQueryModel()
self.returnedTableView.setModel(self.returnedQueryModel)
- 调用已归还书籍查询函数
-
self.returnedQuery()
def returnedQuery(self):
sql = "SELECT Book.BookName,Book.BookId,Auth,Category,Publisher,PublishTime,BorrowTime FROM Book,User_Book WHERE Book.BookId=User_Book.BookId AND User_Book.BorrowState=0 AND StudentId='%s'" % self.studentId
self.returnedQueryModel.setQuery(sql)
return
- 设置列标题
self.returnedQueryModel.setHeaderData(0, Qt.Horizontal, "书名")
self.returnedQueryModel.setHeaderData(1, Qt.Horizontal, "书号")
self.returnedQueryModel.setHeaderData(2, Qt.Horizontal, "作者")
self.returnedQueryModel.setHeaderData(3, Qt.Horizontal, "分类")
self.returnedQueryModel.setHeaderData(4, Qt.Horizontal, "出版社")
self.returnedQueryModel.setHeaderData(5, Qt.Horizontal, "出版时间")
self.returnedQueryModel.setHeaderData(6, Qt.Horizontal, "借阅时间")
self.returnedQueryModel.setHeaderData(7, Qt.Horizontal, "归还时间")
- 已归还页面效果
5、将标签和表格模型分别添加进垂直布局
# 未归还标签
self.layout.addWidget(self.borrowedLabel)
# 未归还表格模型
self.layout.addWidget(self.borrowedTableView)
# 已归还标签
self.layout.addWidget(self.returnedLabel)
# 已归还表格模型
self.layout.addWidget(self.returnedTableView)
return
- 借阅状态整体页面效果
(三)程序入口
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setWindowIcon(QIcon("iron-man.png"))
app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
mainMindow = BorrowStatusViewer("PB15000135")
mainMindow.show()
sys.exit(app.exec_())
二、所有书籍页面
此文档见:
pyqt5图书管理系统--7、管理员页面设计之所有书籍
三、学生界面设计
导入的模块:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon, QFont
from PyQt5 import sip
import qdarkstyle
from BookStorageViewer import BookStorageViewer
from returnBookDialog import returnBookDialog
from borrowBookDialog import borrowBookDialog
from BorrowStatusViewer import BorrowStatusViewer
(一)类基础设置
- 设置页面大小,调用主函数self.setUpUI()
class StudentHome(QWidget):
def __init__(self, studentId):
super(StudentHome, self).__init__()
self.StudentId = studentId
self.resize(900,600)
self.setWindowTitle("欢迎使用图书管理系统")
self.setUpUI()
(二)学生页面总体布局
- 总布局为水平布局 = 按钮的垂直布局 + 所有书籍查询界面的垂直布局
def setUpUI(self):
# 总布局
self.layout = QHBoxLayout(self)
# 按钮布局
self.buttonlayout = QVBoxLayout()
1、设置按钮
- 设置按钮标签
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)
- 设置按钮标签的字体大小
font = QFont()
font.setPixelSize(16)
self.borrowBookButton.setFont(font)
self.returnBookButton.setFont(font)
self.myBookStatus.setFont(font)
self.allBookButton.setFont(font)
- 设置按钮标签的宽高
self.borrowBookButton.setFixedWidth(100)
self.borrowBookButton.setFixedHeight(42)
self.returnBookButton.setFixedHeight(42)
self.returnBookButton.setFixedWidth(100)
self.myBookStatus.setFixedWidth(100)
self.myBookStatus.setFixedHeight(42)
self.allBookButton.setFixedWidth(100)
self.allBookButton.setFixedHeight(42)
2、设置所有书籍查询界面布局
- 实例化查询所有书籍模块
self.storageView = BookStorageViewer()
3、设置借阅状态页面
- 实例化借阅状态模块
self.borrowStatusView = BorrowStatusViewer(self.StudentId)
4、整体页面布局
- 添加按钮垂直布局和所有书籍查询页面布局进入学生主页面
- 默认所有书籍按钮不可点击
self.allBookButton.setEnabled(False)
self.layout.addLayout(self.buttonlayout)
self.layout.addWidget(self.storageView)
(三)信号传递
self.borrowBookButton.clicked.connect(self.borrowBookBunttonClicked)
self.returnBookButton.clicked.connect(self.returnBookBunttonClicked)
self.myBookStatus.clicked.connect(self.myBookStatusClicked)
self.allBookButton.clicked.connect(self.allBookButtonClicked)
1、信号一
- self.borrowBookButton.clicked.connect(self.borrowBookBunttonClicked)
- 绑定连接借阅书籍按钮
- 点击借阅书籍按钮可调用借阅书籍模块
def borrowBookBunttonClicked(self):
borrowDialog = borrowBookDialog(self.StudentId)
borrowDialog.borrow_book_success_signal.connect(self.borrowStatusView.borrowedQuery)
borrowDialog.borrow_book_success_signal.connect(self.storageView.searchButtonClicked)
borrowDialog.show()
borrowDialog.exec_()
2、信号二
- self.returnBookButton.clicked.connect(self.returnBookBunttonClicked)
- 绑定连接归还书籍按钮
- 点击归还书籍按钮可调用归还书籍模块
def returnBookBunttonClicked(self):
returnDialog = returnBookDialog(self.StudentId)
returnDialog.return_book_success_signal.connect(self.borrowStatusView.returnedQuery)
returnDialog.return_book_success_signal.connect(self.storageView.searchButtonClicked)
returnDialog.show()
returnDialog.exec_()
3、信号三
- self.myBookStatus.clicked.connect(self.myBookStatusClicked)
- 绑定连接书籍借阅状态按钮
- 点击借阅状态按钮可调用借阅状态模块,查看书籍借阅状态
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
4、信号四
- self.allBookButton.clicked.connect(self.allBookButtonClicked)
- 绑定连接所有书籍按钮
- 点击所有书籍按钮可调用所有书籍模块,查看所有的书籍
def allBookButtonClicked(self):
self.layout.removeWidget(self.borrowStatusView)
sip.delete(self.borrowStatusView)
self.storageView = BookStorageViewer()
self.borrowStatusView = BorrowStatusViewer(self.StudentId)
self.layout.addWidget(self.storageView)
self.allBookButton.setEnabled(False)
self.myBookStatus.setEnabled(True)
return
(四)程序入口
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setWindowIcon(QIcon('./iron-man.png'))
app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
dr = StudentHome("PB15000135")
dr.show()
sys.exit(app.exec_())
四、完整代码
(一)借阅状态页面代码
借阅状态页面完整代码
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt
import qdarkstyle
from PyQt5.QtSql import *
class BorrowStatusViewer(QWidget):
def __init__(self, StudentId):
super(BorrowStatusViewer, self).__init__()
self.resize(700, 500)
self.studentId = StudentId
self.setWindowTitle("欢迎使用图书管理系统")
self.setUpUI()
def setUpUI(self):
self.db = QSqlDatabase.addDatabase("QSQLITE")
self.db.setDatabaseName('./db/LibraryManagement.db')
self.db.open()
# 分为两块,上方是已借未归还书,下方是已归还书
self.layout = QVBoxLayout(self)
# Label 设置
self.borrowedLabel = QLabel("未归还:")
self.returnedLabel = QLabel("已归还:")
self.borrowedLabel.setFixedHeight(32)
self.borrowedLabel.setFixedWidth(60)
self.returnedLabel.setFixedHeight(32)
self.returnedLabel.setFixedWidth(60)
font = QFont()
font.setPixelSize(18)
self.borrowedLabel.setFont(font)
self.returnedLabel.setFont(font)
# Table 和 Model
self.borrowedTableView = QTableView()
# 设置QT表格的宽度自适应 horizontalHeader() 宽度自适应内容宽度以及填充整个表格
self.borrowedTableView.horizontalHeader().setStretchLastSection(True)
# 自动填充
self.borrowedTableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
# 表格内容不能修改
self.borrowedTableView.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.returnedTableView = QTableView()
self.returnedTableView.horizontalHeader().setStretchLastSection(True)
self.returnedTableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.returnedTableView.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.borrowedQueryModel = QSqlQueryModel()
self.returnedQueryModel = QSqlQueryModel()
self.borrowedTableView.setModel(self.borrowedQueryModel)
self.returnedTableView.setModel(self.returnedQueryModel)
self.borrowedQuery()
self.borrowedQueryModel.setHeaderData(0, Qt.Horizontal, "书名")
self.borrowedQueryModel.setHeaderData(1, Qt.Horizontal, "书号")
self.borrowedQueryModel.setHeaderData(2, Qt.Horizontal, "作者")
self.borrowedQueryModel.setHeaderData(3, Qt.Horizontal, "分类")
self.borrowedQueryModel.setHeaderData(4, Qt.Horizontal, "出版社")
self.borrowedQueryModel.setHeaderData(5, Qt.Horizontal, "出版时间")
self.borrowedQueryModel.setHeaderData(6, Qt.Horizontal, "借出时间")
# self.borrowedQueryModel.setHeaderData(7, Qt.Horizontal, "剩余可借")
# self.borrowedQueryModel.setHeaderData(8, Qt.Horizontal, "总借阅次数")
self.returnedQuery()
self.returnedQueryModel.setHeaderData(0, Qt.Horizontal, "书名")
self.returnedQueryModel.setHeaderData(1, Qt.Horizontal, "书号")
self.returnedQueryModel.setHeaderData(2, Qt.Horizontal, "作者")
self.returnedQueryModel.setHeaderData(3, Qt.Horizontal, "分类")
self.returnedQueryModel.setHeaderData(4, Qt.Horizontal, "出版社")
self.returnedQueryModel.setHeaderData(5, Qt.Horizontal, "出版时间")
self.returnedQueryModel.setHeaderData(6, Qt.Horizontal, "借阅时间")
self.returnedQueryModel.setHeaderData(7, Qt.Horizontal, "归还时间")
# self.returnedQueryModel.setHeaderData(8, Qt.Horizontal, "总借阅次数")
self.layout.addWidget(self.borrowedLabel)
self.layout.addWidget(self.borrowedTableView)
self.layout.addWidget(self.returnedLabel)
self.layout.addWidget(self.returnedTableView)
return
def borrowedQuery(self):
sql = "SELECT Book.BookName,Book.BookId,Auth,Category,Publisher,PublishTime,BorrowTime FROM Book,User_Book WHERE Book.BookId=User_Book.BookId AND User_Book.BorrowState=1 AND StudentId='%s'" % self.studentId
self.borrowedQueryModel.setQuery(sql)
return
def returnedQuery(self):
sql = "SELECT Book.BookName,Book.BookId,Auth,Category,Publisher,PublishTime,BorrowTime FROM Book,User_Book WHERE Book.BookId=User_Book.BookId AND User_Book.BorrowState=0 AND StudentId='%s'" % self.studentId
self.returnedQueryModel.setQuery(sql)
return
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setWindowIcon(QIcon("iron-man.png"))
app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
mainMindow = BorrowStatusViewer("PB15000135")
mainMindow.show()
sys.exit(app.exec_())
(二)学生主页面代码
学生主页面完整代码
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon, QFont
from PyQt5 import sip
import qdarkstyle
from BookStorageViewer import BookStorageViewer
from returnBookDialog import returnBookDialog
from borrowBookDialog import borrowBookDialog
from BorrowStatusViewer import BorrowStatusViewer
class StudentHome(QWidget):
def __init__(self, studentId):
super(StudentHome, self).__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.setFixedHeight(42)
self.returnBookButton.setFixedWidth(100)
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.borrowBookBunttonClicked)
self.returnBookButton.clicked.connect(self.returnBookBunttonClicked)
self.myBookStatus.clicked.connect(self.myBookStatusClicked)
self.allBookButton.clicked.connect(self.allBookButtonClicked)
def borrowBookBunttonClicked(self):
borrowDialog = borrowBookDialog(self.StudentId)
borrowDialog.borrow_book_success_signal.connect(self.borrowStatusView.borrowedQuery)
borrowDialog.borrow_book_success_signal.connect(self.storageView.searchButtonClicked)
borrowDialog.show()
borrowDialog.exec_()
def returnBookBunttonClicked(self):
returnDialog = returnBookDialog(self.StudentId)
returnDialog.return_book_success_signal.connect(self.borrowStatusView.returnedQuery)
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.storageView = BookStorageViewer()
self.borrowStatusView = BorrowStatusViewer(self.StudentId)
self.layout.addWidget(self.storageView)
self.allBookButton.setEnabled(False)
self.myBookStatus.setEnabled(True)
return
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setWindowIcon(QIcon('./iron-man.png'))
app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
dr = StudentHome("PB15000135")
dr.show()
sys.exit(app.exec_())
五、学生页面整体效果展示
- 学生主界面
- 点击借书
- 弹出借阅书籍界面
- 点击还书
- 弹出归还书籍界面
- 点击借阅状态
- 显示未归还书籍信息和已归还书籍信息
待后续更新....