PyQt5图书管理系统练习(9)借书还书功能

环境

系统:windows10系统

编辑器:PyCharm

编程语言:python3+pyqt5

借书逻辑分析

借书跟还书功能有点类似,都是采用的dialog来实现的,同时这个功能借鉴删除书籍的方法,只需输入BookID,就可以得到书籍全部信息。

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

- 用户输入的书号存在,就匹配信息给用户

- 不存在就点击借阅,给出警告

- 借阅书籍数已经达到上限5本 ,给出警告

- 不允许借阅同样的书

- 更新Book表信息的借阅次数,剩余可借阅书数

- 在User_Book表插入记录

- 借阅成功给出提示

程序代码

""""
brief:借书功能
date:2020-10-05
author:chenyijun
version: python V3.8.1 pyqt5 V5.14.0
"""

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import qdarkstyle
import time
from PyQt5.QtSql import *
from initDB import UserBookManager
from initDB import BookDbManager
from initDB import UserDbManager
#import images


class BorrowBookDialog(QDialog):
    borrow_book_success_signal = pyqtSignal()

    def __init__(self, userID, parent=None):
        super(BorrowBookDialog, self).__init__(parent)
        self.userID = userID
        self.setUpUI()
        self.setWindowModality(Qt.WindowModal)
        self.setWindowTitle("借阅书籍")
        self.userbookdb = UserBookManager()  # 借书记录
        self.bookdb = BookDbManager()  # 书籍管理
        self.userdb = UserDbManager()  # 用户管理

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

        # Label控件
        self.borrowStudentLabel = QLabel("借 阅 人:")
        self.borrowuserIDLabel = QLabel(self.userID)
        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.borrowuserIDLabel)
        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.borrowuserIDLabel.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

        bookinfo = self.bookdb.querybyBookID(BookID)
        if (not bookinfo):
            print(QMessageBox.warning(self, "警告", "你所要借的书不存在,请查看输入", QMessageBox.Yes, QMessageBox.Yes))
            return

        # 借书上限5本
        borrowNum = self.userbookdb.countBorrowNum(self.userID)
        if (borrowNum):
            print('節約了几本書= %d' % borrowNum[0][0])
            borrowNum = borrowNum[0][0]
            if (borrowNum >= 5):
                QMessageBox.warning(self, "警告", "您借阅的书达到上限(5本),借书失败!", QMessageBox.Yes, QMessageBox.Yes)
                return

        # 不允许重复借书
        borrowNum = self.userbookdb.borrowStatus(self.userID, BookID)
        print(borrowNum[0][0])
        if (borrowNum[0][0]):
            QMessageBox.warning(self, "警告", "您已经借阅了本书并尚未归还,借阅失败!", QMessageBox.Yes, QMessageBox.Yes)
            return

        # 更新User表
        self.userdb.borrowOrReturnBook(self.userID, borrow=1)

        # 更新Book表
        self.bookdb.borrowOrReturnBook(BookID, borrowflag=1)

        # 插入User_Book表
        timenow = time.strftime('%Y-%m-%d', time.localtime(time.time()))
        self.userbookdb.borrowOrReturnBook(self.userID, BookID, timenow, borrowflag=1)

        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()

        bookinfo = self.bookdb.querybyBookID(BookID)
        # 查询对应书号,如果存在就更新form
        if (bookinfo):
            self.bookNameEdit.setText(bookinfo[0][0])
            self.authNameEdit.setText(bookinfo[0][2])
            self.categoryComboBox.setCurrentText(bookinfo[0][3])
            self.publisherEdit.setText(bookinfo[0][4])
            self.publishTime.setText(bookinfo[0][5])

        return


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

还书的逻辑

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

代码

""""
brief:还书功能
date:2020-10-05
author:chenyijun
version: python V3.8.1 pyqt5 V5.14.0
"""

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import qdarkstyle
import time
#from PyQt5.QtSql import *
from initDB import UserBookManager
from initDB import BookDbManager
from initDB import UserDbManager
#import images


class ReturnBookDialog(QDialog):
    return_book_success_signal = pyqtSignal()

    def __init__(self, userid, parent=None):
        super(ReturnBookDialog, self).__init__(parent)
        self.userid = userid
        self.setUpUI()
        # 设置该窗口为一个当以层次的模态窗口,阻塞它的父窗口、祖父窗口和各个兄弟窗口接受输入信息,此时能够在弹出来的对话框中接收输入法信息。
        self.setWindowModality(Qt.WindowModal)
        self.setWindowTitle("归还书籍")
        self.userbookdb = UserBookManager()  # 借书记录
        self.bookdb = BookDbManager()  # 书籍管理
        self.userdb = UserDbManager()  # 用户管理

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

        # Label控件
        self.returnStudentLabel = QLabel("还 书 人:")
        self.returnuseridLabel = QLabel(self.userid)
        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.returnuseridLabel)
        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.returnuseridLabel.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.setReadOnly(True)
        self.publishTime.setStyleSheet("background-color:#363636")

        self.categoryComboBox.setFont(font)  # QComboBox没有readonly属性
        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)  # 监控输入框
        # self.BookIDEdit.returnPressed.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

        # 如果未借阅
        borrowbook = self.userbookdb.borrowStatus(self.userid, BookID)
        if (not borrowbook[0][0]):
            print(QMessageBox.information(self, "提示", "您并未借阅此书,故无需归还", QMessageBox.Yes, QMessageBox.Yes))
            return

        # 更新User表
        self.userdb.borrowOrReturnBook(self.userid, borrow=0)

        # 更新Book表
        self.bookdb.borrowOrReturnBook(BookID, borrowflag=0)

        # 更新User_Book表
        timenow = time.strftime('%Y-%m-%d', time.localtime(time.time()))
        self.userbookdb.borrowOrReturnBook(self.userid, BookID, timenow, borrowflag=0)

        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()

        # 在User_Book表中找借阅记录,如果存在借阅,则更新form内容
        borrowbook = self.userbookdb.borrowStatus(self.userid, BookID)
        if (borrowbook[0][0]):
            # 根据BookID查询书籍信息,更新form内容

            # 查询对应书号,如果存在就更新form
            bookinfo = self.bookdb.querybyBookID(BookID)
            if (bookinfo):
                self.bookNameEdit.setText(bookinfo[0][0])
                self.authNameEdit.setText(bookinfo[0][2])
                self.categoryComboBox.setCurrentText(bookinfo[0][3])
                self.publisherEdit.setText(bookinfo[0][4])
                self.publishTime.setText(bookinfo[0][5])
            return


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

参考:

https://blog.csdn.net/weixin_38312031/article/details/80153133

https://blog.csdn.net/qiqiyingse/article/details/88240335

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值