#此代码设计借阅书籍的界面
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import qdarkstyle
from PyQt5.QtSql import *
import time
# 创建借阅图书的类
class borrowBookDialog(QDialog):
borrow_book_success_signal = pyqtSignal()
def __init__(self, StudentId):
super(borrowBookDialog, self).__init__()
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.publisherDateLabel = QLabel("出版日期:")
# button控件
self.borrowBookButton = QPushButton("确认借阅")
# lineEdit控件
self.bookNameEdit = QLineEdit()
self.bookIdEdit = QLineEdit()
self.authNameEdit = QLineEdit()
# 分类创建的QComboBox,存储整个分类的列表
# QComboBox 以占用最少屏幕控件的方式向用户呈现选项列表的方法
self.categoryEdit = QComboBox()
self.categoryEdit.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.bookNameLabel, self.bookNameEdit)
self.layout.addRow(self.bookIdLabel, self.bookIdEdit)
self.layout.addRow(self.authNameLabel, self.authNameEdit)
self.layout.addRow(self.categoryLabel, self.categoryEdit)
self.layout.addRow(self.publisherLabel, self.publisherEdit)
self.layout.addRow(self.publisherDateLabel, self.publishTime)
self.layout.addRow("", self.borrowBookButton)
# 设置字体
font = QFont()
font.setPixelSize(20)
self.titleLabel.setFont(font)
font.setPixelSize(14)
self.bookNameLabel.setFont(font)
self.bookIdLabel.setFont(font)
self.authNameLabel.setFont(font)
self.categoryLabel.setFont(font)
self.publisherLabel.setFont(font)
self.publisherDateLabel.setFont(font)
self.bookNameEdit.setFont(font)
# 只能读取
self.bookNameEdit.setReadOnly(True)
self.bookNameEdit.setStyleSheet("background-color:grey")
# 通过输入ID返回其余的所有信息
self.bookIdEdit.setFont(font)
# 设置Qt的css代码,修改控件的背景颜色
self.authNameEdit.setFont(font)
self.authNameEdit.setReadOnly(True)
self.authNameEdit.setStyleSheet("background-color:grey")
self.publisherEdit.setFont(font)
self.publisherEdit.setReadOnly(True)
self.publisherEdit.setStyleSheet("background-color:grey")
self.publishTime.setFont(font)
self.publishTime.setReadOnly(True)
self.publishTime.setStyleSheet("background-color:grey")
self.categoryEdit.setFont(font)
# self.categoryEdit.setReadOnly(True)
self.categoryEdit.setStyleSheet("background-color:grey")
# 设置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.borrwoButtonClicked)
self.bookIdEdit.textChanged.connect(self.bookIdEditChange)
self.bookIdEdit.returnPressed.connect(self.borrwoButtonClicked)
def borrwoButtonClicked(self):
"""
获取书号,书号为空或者不存在库中,则弹出错误
向book_user表中插入记录,更新user表以及book表
:return:
"""
BookId = self.bookIdEdit.text()
# BookId为空的处理
if BookId == "":
print(QMessageBox.warning(self, '警告', '你所要借阅的书籍不存在,请重新输入', QMessageBox.Yes,
QMessageBox.Yes))
return
# 数据库
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
# 不允许借重复的书籍
print(BookId)
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 bookIdEditChange(self):
bookID = self.bookIdEdit.text()
if bookID == "":
self.bookNameEdit.clear()
self.authNameEdit.clear()
self.publisherEdit.clear()
self.publishTime.clear()
self.dropNumEdit.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)
if query.next():
self.bookNameEdit.setText(query.value(0))
self.authNameEdit.setText(query.value(2))
self.categoryEdit.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("./iron-man.png"))
app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
mainMindow = borrowBookDialog("PB15000000")
mainMindow.show()
sys.exit(app.exec_())
PYQT5开发图书管理系统(4)
最新推荐文章于 2023-07-16 19:06:05 发布