乐之之

知而行乐,乐而行之,天道酬勤,学无止境。
pyqt5图书管理系统--4、管理员页面设计之淘汰书籍

本节分为两个部分:管理员界面设计、淘汰书籍界面设计。

主要流程:1、通过进入管理员界面,点击淘汰书籍按钮,转到淘汰书籍界面。

     2、连接数据库,进行逻辑输入。

       3、淘汰书籍时,输入书号,返回显示的书籍对应信息。

       4、点击淘汰按钮,对书籍所存在的可删除数量进行相应提示。

一、管理员界面

  • 新添加模块:
from dropBookDialog import dropBookDialog
  • 导入dropBookDialog模块
  • 调用淘汰书籍模块
    self.dropBookButton.clicked.connect(self.dropBookButtonClicked)
    def dropBookButtonClicked(self):
        dropDialog = dropBookDialog()
        dropDialog.show()
        dropDialog.exec_()

二、淘汰书籍界面

  • 模块导入
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import qdarkstyle
from PyQt5.QtSql import *
import time

(一)类基础设置

class dropBookDialog(QDialog):
    # 用于接收信号
    drop_book_successful_signal = pyqtSignal()

    def __init__(self):
        super(dropBookDialog, self).__init__()
        self.setWindowModality(Qt.WindowModal)
        self.setWindowTitle("删除书籍")
        self.setUpUI()

 (二)淘汰界面设计

1、页面大小布局

def setUpUI(self):

    self.resize(300, 400)
    self.layout = QFormLayout()
    self.setLayout(self.layout)

 2、添加标签及大小

(1)添加书籍标签

# Label标签控件
self.titleLabel = QLabel(" 淘汰书籍")
self.bookNameLabel = QLabel("书 名:")
self.bookIdLabel = QLabel("书 号:")
self.authNameLabel = QLabel("作 者:")
self.categoryLabel = QLabel("分 类:")
self.publisherLabel = QLabel("出 版 社:")
self.publisherDateLabel = QLabel("出版日期:")
self.dropNumLabel = QLabel("数 量:")

# button控件
self.dropBookButton = QPushButton("淘汰")

(2)设置书籍标签字体的大小

# 设置字体
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.dropNumLabel.setFont(font)

(3)设置按钮大小

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

 3、输入框设置

(1)添加输入框

  • 分类输入框的类型为:选项输入框类型
  BookCategory = ["哲学","教育","生物学","社会科学","政治", "法律","军事","经济","文化","体育","语言文字","地理","天文学","医疗卫生","农业"]
self.bookNameEdit = QLineEdit()
self.bookIdEdit = QLineEdit()
self.authNameEdit = QLineEdit()
# 分类创建的QComboBox,存储整个分类的列表
# QComboBox 以占用最少屏幕控件的方式向用户呈现选项列表的方法
self.categoryEdit = QComboBox()
self.categoryEdit.addItems(BookCategory)
self.publisherEdit = QLineEdit()

# QDateTimeEdit时间控件
# setDisplayFormat 设置显示的格式 yyyy-MM-dd 年-月-日
self.publishTime = QLineEdit()
# self.publishTime.setDisplayFormat("yyyy-MM-dd")
self.dropNumEdit = QLineEdit()

(2)设置输入框可输入的长度

# 限制输入的长度
self.bookNameEdit.setMaxLength(10)
self.bookIdEdit.setMaxLength(6)
self.authNameEdit.setMaxLength(10)
self.publisherEdit.setMaxLength(10)
self.dropNumEdit.setMaxLength(12)
# 设置输入框内为整数
self.dropNumEdit.setValidator(QIntValidator())

(3)设置输入框的字体、背景、可输入方式

font.setPixelSize(18)
self.bookNameEdit.setFont(font)
# 只能读取
self.bookNameEdit.setReadOnly(True)
# 设置Qt的css代码,修改控件的背景颜色
self.bookNameEdit.setStyleSheet("background-color:grey")

self.bookIdEdit.setFont(font)
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")

self.dropNumEdit.setFont(font)

 4、设置标签的间距

# 设置间距
self.titleLabel.setMargin(8)
# 垂直布局上下行之间的间距
self.layout.setVerticalSpacing(10)

 5、表单布局

# 添加进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.dropNumLabel, self.dropNumEdit)
self.layout.addRow("", self.dropBookButton)

(三)信号传递

1、信号一

  • 通过书号id查询书籍相应信息。
self.bookIdEdit.textChanged.connect(self.bookIdEditChange)

(1)书号输入框id接收信号,调用查询书号id的函数。

def bookIdEditChange(self):
    bookID = self.bookIdEdit.text()
    # 检测书号id是否为空,若为空则清楚所有输入框信息
    if bookID == "":
        self.bookNameEdit.clear()
        self.authNameEdit.clear()
        self.publisherEdit.clear()
        self.publishTime.clear()
        self.dropNumEdit.clear()

 (2)打开数据库,查询对应书号

db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName("./db/LibraryManagement.db")
db.open()
query = QSqlQuery()
sql = "select * from Book where BookId='%s'" % (bookID)
query.exec_(sql)

 (3)如果存在就更新输入框数据

# 查询对应书号,如果存在就更新form

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

 

2、信号二

  • 通过淘汰按钮,删除书籍。
self.dropBookButton.clicked.connect(self.dropBookButtonClicked)

 (1)淘汰按钮接收信号,触发淘汰按钮函数。

# 点击淘汰触发删除事件
def dropBookButtonClicked(self):
    # 获取书号信息
    bookId = self.bookIdEdit.text()

    # 若删除书籍数量输入框为空,则进行提示
    if self.dropNumEdit.text() == "":
        print(QMessageBox.warning(self, "警告", "淘汰数目为空, 请检查输入,操作失败"), QMessageBox.Yes, QMessageBox.Yes)

 (2)连接数据库

# 将删除书籍输入框内的数据类型转化为整数型
dropNum =  int(self.dropNumEdit.text())
# 连接数据库
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName("./db/LibraryManagement.db")
db.open()
query = QSqlQuery()
# 查询书籍书号
sql = "select * from Book where BookId='%s'" % (bookId)
query.exec_(sql)

 (3)逻辑判断

# 若书号存在,执行if函数内语句
if query.next():
    # 若删除书籍数量大于可借阅存在的数量,或输出数量小于0,进行弹窗提示。
    if (dropNum > query.value(7) or dropNum < 0):
        print(QMessageBox.warning(self, "警告", f"最多可淘汰{query.value(7)}本, 请检查输入!"),QMessageBox.Yes, QMessageBox.Yes)
        return
    # 如果drop数目与当前库存相同,则直接删除Book记录(这里默认当前所有书都在库存中)
if dropNum == query.value(7):
    sql = "delete from Book where BookId='%s'" %(bookId)

else:
    sql = "update Book set NumStorage=NumStorage-%d, NumCanBorrow=NumCanBorrow-%d where BookId='%s'" % (dropNum, dropNum, bookId)
query.exec_(sql)
db.commit()

 (4)添加书籍处理信息表信息

  • 删除的同时在书籍处理信息表中,添加处理书籍的书号、处理时的时间、处理的数量 。
  • 删除成功后进行消息框提示。
timenow = time.strftime("%Y-%m-%d", time.localtime(time.time()))
sql = "insert into BuyorDrop values ('%s', '%s', 0, %d)" %(bookId, timenow, dropNum)
query.exec_(sql)
db.commit()
print(QMessageBox.information(self, "提示", "淘汰书籍成功", QMessageBox.Yes, QMessageBox.Yes))
# 信号传递
self.drop_book_successful_signal.emit()
self.close()
return

(四)程序入口

if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setWindowIcon(QIcon('./iron-man.png'))
    app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
    dr = dropBookDialog()
    dr.show()
    sys.exit(app.exec_())

三、完整代码

(一)管理员界面完整代码

import sys
from PyQt5.QtWidgets import *
import qdarkstyle
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtSql import *
from addBookDialog import addBookDialog
from dropBookDialog import dropBookDialog
class AdminHome(QWidget):
    def __init__(self):
        super(AdminHome, self).__init__()
        self.setUpUi()

    def setUpUi(self):
        self.resize(900,600)
        self.setWindowTitle("欢迎进入管理员主页")
        self.layout = QHBoxLayout()
        self.buttonlayout = QVBoxLayout()
        self.setLayout(self.layout)

        font = QFont()
        font.setPixelSize(16)

        self.userManageButton = QPushButton("用户管理")
        self.addBookButton = QPushButton("添加书籍")
        self.dropBookButton = QPushButton("淘汰书籍")

        self.userManageButton.setFont(font)
        self.addBookButton.setFont(font)
        self.dropBookButton.setFont(font)

        self.userManageButton.setFixedWidth(100)
        self.userManageButton.setFixedHeight(42)
        self.addBookButton.setFixedWidth(100)
        self.addBookButton.setFixedHeight(42)
        self.dropBookButton.setFixedWidth(100)
        self.dropBookButton.setFixedHeight(42)
        # 将三个按钮添加到垂直布局中
        self.buttonlayout.addWidget(self.addBookButton)
        self.buttonlayout.addWidget(self.userManageButton)
        self.buttonlayout.addWidget(self.dropBookButton)
        # 将按钮布局添加到垂直布局中
        self.layout.addLayout(self.buttonlayout)

        self.addBookButton.clicked.connect(self.addBookButtonClicked)
        self.dropBookButton.clicked.connect(self.dropBookButtonClicked)
    def addBookButtonClicked(self):
        addDialog = addBookDialog()
        addDialog.show()
        addDialog.exec_()
    def dropBookButtonClicked(self):
        dropDialog = dropBookDialog()
        dropDialog.show()
        dropDialog.exec_()
if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
    app.setWindowIcon(QIcon('./iron-man.png'))
    mianMindow = AdminHome()
    mianMindow.show()
    sys.exit(app.exec_())

 (二)淘汰书籍界面完整代码

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import qdarkstyle
from PyQt5.QtSql import *
import time


class dropBookDialog(QDialog):
    drop_book_successful_signal = pyqtSignal()

    def __init__(self):
        super(dropBookDialog, self).__init__()
        self.setWindowModality(Qt.WindowModal)
        self.setWindowTitle("删除书籍")
        self.setUpUI()

    def setUpUI(self):
        BookCategory = ["哲学", "教育", "生物学", "社会科学", "政治",
                        "法律", "军事", "经济", "文化", "体育", "语言文字", "地理", "天文学", "医学卫生", "农业"]

        self.resize(300, 400)
        self.layout = QFormLayout()
        self.setLayout(self.layout)

        # Label标签控件
        self.titleLabel = QLabel(" 淘汰书籍")
        self.bookNameLabel = QLabel("书 名:")
        self.bookIdLabel = QLabel("书 号:")
        self.authNameLabel = QLabel("作 者:")
        self.categoryLabel = QLabel("分 类:")
        self.publisherLabel = QLabel("出 版 社:")
        self.publisherDateLabel = QLabel("出版日期:")
        self.dropNumLabel = QLabel("数 量:")

        # button控件
        self.dropBookButton = QPushButton("淘汰")

        # lineEdit控件
        self.bookNameEdit = QLineEdit()
        self.bookIdEdit = QLineEdit()
        self.authNameEdit = QLineEdit()
        # 分类创建的QComboBox,存储整个分类的列表
        # QComboBox 以占用最少屏幕控件的方式向用户呈现选项列表的方法
        self.categoryEdit = QComboBox()
        self.categoryEdit.addItems(BookCategory)
        self.publisherEdit = QLineEdit()

        # QDateTimeEdit时间控件
        # setDisplayFormat 设置显示的格式 yyyy-MM-dd 年-月-日
        self.publishTime = QLineEdit()
        # self.publishTime.setDisplayFormat("yyyy-MM-dd")
        self.dropNumEdit = QLineEdit()

        # 限制输入的长度
        self.bookNameEdit.setMaxLength(10)
        self.bookIdEdit.setMaxLength(6)
        self.authNameEdit.setMaxLength(10)
        self.publisherEdit.setMaxLength(10)
        self.dropNumEdit.setMaxLength(12)
        # 设置输入框内为整数
        self.dropNumEdit.setValidator(QIntValidator())

        # 添加进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.dropNumLabel, self.dropNumEdit)
        self.layout.addRow("", self.dropBookButton)

        # 设置字体
        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.dropNumLabel.setFont(font)

        font.setPixelSize(18)
        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")

        self.dropNumEdit.setFont(font)

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

        # 设置间距
        self.titleLabel.setMargin(8)
        # 垂直布局上下行之间的间距
        self.layout.setVerticalSpacing(10)

        # 触发id,读取其他所有内容
        self.dropBookButton.clicked.connect(self.dropBookButtonClicked)
        self.bookIdEdit.textChanged.connect(self.bookIdEditChange)
    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)

        # 查询对应书号,如果存在就更新form

        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

    # 点击淘汰触发删除事件
    def dropBookButtonClicked(self):
        bookId = self.bookIdEdit.text()
        dropNum = 0
        if self.dropNumEdit.text() == "":
            print(QMessageBox.warning(self, "警告", "淘汰数目为空, 请检查输入,操作失败"), QMessageBox.Yes, QMessageBox.Yes)


        dropNum =  int(self.dropNumEdit.text())

        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():
            if (dropNum > query.value(7) or dropNum < 0):
                print(QMessageBox.warning(self, "警告", f"最多可淘汰{query.value(7)}本, 请检查输入!"),QMessageBox.Yes, QMessageBox.Yes)
                return
            # 如果drop数目与当前库存相同,则直接删除Book记录(这里默认当前所有书都在库存中)
        if dropNum == query.value(7):
            sql = "delete from Book where BookId='%s'" %(bookId)
        else:
            sql = "update Book set NumStorage=NumStorage-%d, NumCanBorrow=NumCanBorrow-%d where BookId='%s'" % (dropNum, dropNum, bookId)
        query.exec_(sql)
        db.commit()

        timenow = time.strftime("%Y-%m-%d", time.localtime(time.time()))
        sql = "insert into BuyorDrop values ('%s', '%s', 0, %d)" %(bookId, timenow, dropNum)
        query.exec_(sql)
        db.commit()
        print(QMessageBox.information(self, "提示", "淘汰书籍成功", QMessageBox.Yes, QMessageBox.Yes))
        self.drop_book_successful_signal.emit()
        self.close()
        return


if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setWindowIcon(QIcon('./iron-man.png'))
    app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
    dr = dropBookDialog()
    dr.show()
    sys.exit(app.exec_())

四、效果展示

(一)管理员界面效果展示

  • 管理员主界面

  • 点击淘汰书籍显示淘汰书籍窗口 

(二)淘汰书籍界面效果展示

  • 不输入消息提示

 

  •  查询书号界面效果

  • 删除数量大于库存量消息提示

  • 删除书籍成功消息提示

(三)数据库效果展示

  • 未找到书号为IS1000的书籍,删除成功!

  • 书籍信息处理记录表信息记录成功

待后续更新....

posted on 2022-12-12 19:16  乐之之  阅读(87)  评论(0编辑  收藏  举报