PyQt5搭建图书馆管理系统(8)——借书/还书功能

借书功能实现#

分析#

本来借书的信息都是应该有RFID识别得到的,但是作为大作业,只能让用户输入信息了,但是书名之类的都不唯一,所以采取了与淘汰书籍一样的操作——让用户输入书号,自动匹配书本信息,然后确认借阅

借阅时,需要处理的逻辑如下:

  • 用户输入的书号存在,就匹配信息给用户
  • 不存在就点击借阅,给出警告
  • 借阅书籍数已经达到上限5本 ,给出警告
  • 不允许借阅同样的书
  • 更新Book表信息的借阅次数,剩余可借阅书数
  • User_Book表插入记录
  • 借阅成功给出提示

代码实现#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import qdarkstyle
import time
from PyQt5.QtSql import *


class borrowBookDialog(QDialog):
borrow_book_success_signal = pyqtSignal()

def __init__(self, StudentId, parent=None):
super(borrowBookDialog, self).__init__(parent)
self.studentId = StudentId
self.setUpUI()
self.setWindowModality(Qt.WindowModal)
self.setWindowTitle("借阅书籍")

def setUpUI(self):
# 书名,书号,作者,分类,添加数量.出版社,出版日期
# 书籍分类:哲学类、社会科学类、政治类、法律类、军事类、经济类、文化类、教育类、体育类、语言文字类、艺术类、历史类、地理类、天文学类、生物学类、医学卫生类、农业类
BookCategory = ["哲学", "社会科学", "政治", "法律", "军事", "经济", "文化", "教育", "体育", "语言文字", "艺术", "历史"
, "地理", "天文学", "生物学", "医学卫生", "农业"]
self.resize(300, 400)
self.layout = QFormLayout()
self.setLayout(self.layout)

# Label控件
self.borrowStudentLabel = QLabel("借 阅 人:")
self.borrowStudentIdLabel = QLabel(self.studentId)
self.titlelabel = QLabel(" 借阅书籍")
self.bookNameLabel = QLabel("书 名:")
self.bookIdLabel = QLabel("书 号:")
self.authNameLabel = QLabel("作 者:")
self.categoryLabel = QLabel("分 类:")
self.publisherLabel = QLabel("出 版 社:")
self.publishDateLabel = QLabel("出版日期:")

# button控件
self.borrowBookButton = QPushButton("确认借阅")

# lineEdit控件
self.bookNameEdit = QLineEdit()
self.bookIdEdit = QLineEdit()
self.authNameEdit = QLineEdit()
self.categoryComboBox = QComboBox()
self.categoryComboBox.addItems(BookCategory)
self.publisherEdit = QLineEdit()
self.publishTime = QLineEdit()

self.bookNameEdit.setMaxLength(10)
self.bookIdEdit.setMaxLength(6)
self.authNameEdit.setMaxLength(10)
self.publisherEdit.setMaxLength(10)

# 添加进formlayout
self.layout.addRow("", self.titlelabel)
self.layout.addRow(self.borrowStudentLabel, self.borrowStudentIdLabel)
self.layout.addRow(self.bookNameLabel, self.bookNameEdit)
self.layout.addRow(self.bookIdLabel, self.bookIdEdit)
self.layout.addRow(self.authNameLabel, self.authNameEdit)
self.layout.addRow(self.categoryLabel, self.categoryComboBox)
self.layout.addRow(self.publisherLabel, self.publisherEdit)
self.layout.addRow(self.publishDateLabel, self.publishTime)
self.layout.addRow("", self.borrowBookButton)

# 设置字体
font = QFont()
font.setPixelSize(20)
self.titlelabel.setFont(font)
font.setPixelSize(16)
self.borrowStudentIdLabel.setFont(font)
font.setPixelSize(14)
self.borrowStudentLabel.setFont(font)
self.bookNameLabel.setFont(font)
self.bookIdLabel.setFont(font)
self.authNameLabel.setFont(font)
self.categoryLabel.setFont(font)
self.publisherLabel.setFont(font)
self.publishDateLabel.setFont(font)

self.bookNameEdit.setFont(font)
self.bookNameEdit.setReadOnly(True)
self.bookNameEdit.setStyleSheet("background-color:#363636")
self.bookIdEdit.setFont(font)
self.authNameEdit.setFont(font)
self.authNameEdit.setReadOnly(True)
self.authNameEdit.setStyleSheet("background-color:#363636")
self.publisherEdit.setFont(font)
self.publisherEdit.setReadOnly(True)
self.publisherEdit.setStyleSheet("background-color:#363636")
self.publishTime.setFont(font)
self.publishTime.setStyleSheet("background-color:#363636")
self.categoryComboBox.setFont(font)
self.categoryComboBox.setStyleSheet("background-color:#363636")

# button设置
font.setPixelSize(16)
self.borrowBookButton.setFont(font)
self.borrowBookButton.setFixedHeight(32)
self.borrowBookButton.setFixedWidth(140)

# 设置间距
self.titlelabel.setMargin(8)
self.layout.setVerticalSpacing(10)
self.borrowBookButton.clicked.connect(self.borrowButtonClicked)
self.bookIdEdit.textChanged.connect(self.bookIdEditChanged)
self.bookIdEdit.returnPressed.connect(self.borrowButtonClicked)

def borrowButtonClicked(self):
# 获取书号,书号为空或不存在库中,则弹出错误
# 向Book_User表插入记录,更新User表以及Book表
BookId = self.bookIdEdit.text()
# BookId为空的处理
if (BookId == ""):
print(QMessageBox.warning(self, "警告", "你所要借的书不存在,请查看输入", QMessageBox.Yes, QMessageBox.Yes))
return
# 打开数据库
db = db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName('./db/LibraryManagement.db')
db.open()
query = QSqlQuery()
# 如果BookId不存在
sql = "SELECT * FROM Book WHERE BookId='%s'" % BookId
query.exec_(sql)
if (not query.next()):
print(QMessageBox.warning(self, "警告", "你所要借的书不存在,请查看输入", QMessageBox.Yes, QMessageBox.Yes))
return

# 借书上限5本
sql = "SELECT COUNT(StudentId) FROM User_Book WHERE StudentId='%s' AND BorrowState=1" % (
self.studentId)
query.exec_(sql)
if (query.next()):
borrowNum = query.value(0)
if (borrowNum == 5):
QMessageBox.warning(self, "警告", "您借阅的书达到上限(5本),借书失败!", QMessageBox.Yes, QMessageBox.Yes)
return
# 不允许重复借书
sql = "SELECT COUNT(StudentId) FROM User_Book WHERE StudentId='%s' AND BookId='%s' AND BorrowState=1" % (
self.studentId, BookId)
query.exec_(sql)
if (query.next() and query.value(0)):
QMessageBox.warning(self, "警告", "您已经借阅了本书并尚未归还,借阅失败!", QMessageBox.Yes, QMessageBox.Yes)
return
# 更新User表
sql = "UPDATE User SET TimesBorrowed=TimesBorrowed+1,NumBorrowed=NumBorrowed+1 WHERE StudentId='%s'" % self.studentId
query.exec_(sql)
db.commit()
# 更新Book表
sql = "UPDATE Book SET NumCanBorrow=NumCanBorrow-1,NumBorrowed=NumBorrowed+1 WHERE BookId='%s'" % BookId
query.exec_(sql)
db.commit()
# 插入User_Book表
timenow = time.strftime('%Y-%m-%d', time.localtime(time.time()))
sql = "INSERT INTO User_Book VALUES ('%s','%s','%s',NULL,1)" % (self.studentId, BookId, timenow)
print(sql)
query.exec_(sql)
db.commit()
print(QMessageBox.information(self, "提示", "借阅成功!", QMessageBox.Yes, QMessageBox.Yes))
self.borrow_book_success_signal.emit()
self.close()
return

def bookIdEditChanged(self):
bookId = self.bookIdEdit.text()
if (bookId == ""):
self.bookNameEdit.clear()
self.publisherEdit.clear()
self.authNameEdit.clear()
self.publishTime.clear()
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName('./db/LibraryManagement.db')
db.open()
query = QSqlQuery()
sql = "SELECT * FROM Book WHERE BookId='%s'" % (bookId)
query.exec_(sql)
# 查询对应书号,如果存在就更新form
if (query.next()):
self.bookNameEdit.setText(query.value(0))
self.authNameEdit.setText(query.value(2))
self.categoryComboBox.setCurrentText(query.value(3))
self.publisherEdit.setText(query.value(4))
self.publishTime.setText(query.value(5))
return


if __name__ == "__main__":
app = QApplication(sys.argv)
app.setWindowIcon(QIcon("./images/MainWindow_1.png"))
app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
mainMindow = borrowBookDialog("PB15000135")
mainMindow.show()
sys.exit(app.exec_())

实现效果#

这里写图片描述
这里写图片描述
这里写图片描述

归还书籍功能实现#

分析#

一开始的时候被借书功能带偏了,依旧使用了让用户输入书号的方式,但其实使用QComboBox列出已借阅图书让用户选择更好,大家可以自己试试

实现逻辑如下:

  • 如果存在借阅记录,就自动匹配书籍信息
  • 如果输入为空,给出警告
  • 并未借阅,给出提示
  • 更新User表,Book表以及User_Book

实现代码#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import qdarkstyle
import time
from PyQt5.QtSql import *


class returnBookDialog(QDialog):
return_book_success_signal=pyqtSignal()
def __init__(self, StudentId, parent=None):
super(returnBookDialog, self).__init__(parent)
self.studentId = StudentId
self.setUpUI()
self.setWindowModality(Qt.WindowModal)
self.setWindowTitle("归还书籍")

def setUpUI(self):
# 书名,书号,作者,分类,添加数量.出版社,出版日期
# 书籍分类:哲学类、社会科学类、政治类、法律类、军事类、经济类、文化类、教育类、体育类、语言文字类、艺术类、历史类、地理类、天文学类、生物学类、医学卫生类、农业类
BookCategory = ["哲学", "社会科学", "政治", "法律", "军事", "经济", "文化", "教育", "体育", "语言文字", "艺术", "历史"
, "地理", "天文学", "生物学", "医学卫生", "农业"]
self.resize(300, 400)
self.layout = QFormLayout()
self.setLayout(self.layout)

# Label控件
self.returnStudentLabel = QLabel("还 书 人:")
self.returnStudentIdLabel = QLabel(self.studentId)
self.titlelabel = QLabel(" 归还书籍")
self.bookNameLabel = QLabel("书 名:")
self.bookIdLabel = QLabel("书 号:")
self.authNameLabel = QLabel("作 者:")
self.categoryLabel = QLabel("分 类:")
self.publisherLabel = QLabel("出 版 社:")
self.publishDateLabel = QLabel("出版日期:")

# button控件
self.returnBookButton = QPushButton("确认归还")

# lineEdit控件
self.bookNameEdit = QLineEdit()
self.bookIdEdit = QLineEdit()
self.authNameEdit = QLineEdit()
self.categoryComboBox = QComboBox()
self.categoryComboBox.addItems(BookCategory)
self.publisherEdit = QLineEdit()
self.publishTime = QLineEdit()

self.bookNameEdit.setMaxLength(10)
self.bookIdEdit.setMaxLength(6)
self.authNameEdit.setMaxLength(10)
self.publisherEdit.setMaxLength(10)

# 添加进formlayout
self.layout.addRow("", self.titlelabel)
self.layout.addRow(self.returnStudentLabel, self.returnStudentIdLabel)
self.layout.addRow(self.bookNameLabel, self.bookNameEdit)
self.layout.addRow(self.bookIdLabel, self.bookIdEdit)
self.layout.addRow(self.authNameLabel, self.authNameEdit)
self.layout.addRow(self.categoryLabel, self.categoryComboBox)
self.layout.addRow(self.publisherLabel, self.publisherEdit)
self.layout.addRow(self.publishDateLabel, self.publishTime)
self.layout.addRow("", self.returnBookButton)

# 设置字体
font = QFont()
font.setPixelSize(20)
self.titlelabel.setFont(font)
font.setPixelSize(16)
self.returnStudentIdLabel.setFont(font)
font.setPixelSize(14)
self.returnStudentLabel.setFont(font)
self.bookNameLabel.setFont(font)
self.bookIdLabel.setFont(font)
self.authNameLabel.setFont(font)
self.categoryLabel.setFont(font)
self.publisherLabel.setFont(font)
self.publishDateLabel.setFont(font)

self.bookNameEdit.setFont(font)
self.bookNameEdit.setReadOnly(True)
self.bookNameEdit.setStyleSheet("background-color:#363636")
self.bookIdEdit.setFont(font)
self.authNameEdit.setFont(font)
self.authNameEdit.setReadOnly(True)
self.authNameEdit.setStyleSheet("background-color:#363636")
self.publisherEdit.setFont(font)
self.publisherEdit.setReadOnly(True)
self.publisherEdit.setStyleSheet("background-color:#363636")
self.publishTime.setFont(font)
self.publishTime.setStyleSheet("background-color:#363636")
self.categoryComboBox.setFont(font)
self.categoryComboBox.setStyleSheet("background-color:#363636")

# button设置
font.setPixelSize(16)
self.returnBookButton.setFont(font)
self.returnBookButton.setFixedHeight(32)
self.returnBookButton.setFixedWidth(140)

# 设置间距
self.titlelabel.setMargin(8)
self.layout.setVerticalSpacing(10)
self.returnBookButton.clicked.connect(self.returnButtonClicked)
self.bookIdEdit.textChanged.connect(self.bookIdEditChanged)

def returnButtonClicked(self):
# 获取书号,书号为空或并未借阅,则弹出错误
# 更新Book_User表User表以及Book表
BookId = self.bookIdEdit.text()
# BookId为空的处理
if (BookId == ""):
print(QMessageBox.warning(self, "警告", "你所要还的书不存在,请查看输入", QMessageBox.Yes, QMessageBox.Yes))
return
# 打开数据库
db = db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName('./db/LibraryManagement.db')
db.open()
query = QSqlQuery()
# 如果未借阅
sql = "SELECT * FROM User_Book WHERE StudentId='%s' AND BookId='%s' AND BorrowState=1" %(self.studentId,BookId)
query.exec_(sql)
if (not query.next()):
print(QMessageBox.information(self, "提示", "您并未借阅此书,故无需归还", QMessageBox.Yes, QMessageBox.Yes))
return
# 更新User表
sql = "UPDATE User SET NumBorrowed=NumBorrowed-1 WHERE StudentId='%s'" % self.studentId
query.exec_(sql)
db.commit()
# 更新Book表
sql = "UPDATE Book SET NumCanBorrow=NumCanBorrow+1 WHERE BookId='%s'" % BookId
query.exec_(sql)
db.commit()
# 更新User_Book表
timenow = time.strftime('%Y-%m-%d', time.localtime(time.time()))
sql = "UPDATE User_Book SET ReturnTime='%s',BorrowState=0 WHERE StudentId='%s' AND BookId='%s' AND BorrowState=1" % (timenow,self.studentId,BookId)
query.exec_(sql)
db.commit()
print(QMessageBox.information(self, "提示", "归还成功!", QMessageBox.Yes, QMessageBox.Yes))
self.return_book_success_signal.emit()
self.close()
return

def bookIdEditChanged(self):
bookId = self.bookIdEdit.text()
if (bookId == ""):
self.bookNameEdit.clear()
self.publisherEdit.clear()
self.authNameEdit.clear()
self.publishTime.clear()
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName('./db/LibraryManagement.db')
db.open()
query = QSqlQuery()
# 在User_Book表中找借阅记录,如果存在借阅,则更新form内容
sql = "SELECT * FROM User_Book WHERE StudentId='%s' AND BookId='%s' AND BorrowState=1" % (
self.studentId, bookId)
query.exec_(sql)
if (query.next()):
# 更新form内容
sql = "SELECT * FROM Book WHERE BookId='%s'" % (bookId)
query.exec_(sql)
# 查询对应书号,如果存在就更新form
if (query.next()):
self.bookNameEdit.setText(query.value(0))
self.authNameEdit.setText(query.value(2))
self.categoryComboBox.setCurrentText(query.value(3))
self.publisherEdit.setText(query.value(4))
self.publishTime.setText(query.value(5))
return


if __name__ == "__main__":
app = QApplication(sys.argv)
app.setWindowIcon(QIcon("./images/MainWindow_1.png"))
app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
mainMindow = returnBookDialog("PB15000135")
mainMindow.show()
sys.exit(app.exec_())

实现效果#

这里写图片描述
这里写图片描述
这里写图片描述

最后附上链接#

项目地址:github仓库链接

评论