首页 > 其他分享 >pyqt5图书管理系统--3、管理员页面设计之淘汰书籍

pyqt5图书管理系统--3、管理员页面设计之淘汰书籍

时间:2022-12-12 19:22:46浏览次数:49  
标签:layout -- self db pyqt5 setFont query font 页面

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

主要流程: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的书籍,删除成功!

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

待后续更新....

标签:layout,--,self,db,pyqt5,setFont,query,font,页面
From: https://www.cnblogs.com/LoLong/p/16976912.html

相关文章

  • rocketmq docker-compose版安装与配置
    因为在本地使用rocketmq,资源开销实在太大,8+4G,所有选择了个docker版本,用了不到2G。如下步骤。在服务器上建立一个目录 rocketmq_docker1、建立目录。logs==日志store=......
  • android开发新版Android studio使用新版logcat过滤自己app的某个进程的log信息的解决
    查看自己app进程列表,可能是多进程,都可以查看出来adbshell"ps-Af|grepcom.suyf.demo"#com.suyf.demo是自己app的包名,即可查看自己包名的所有进程pid比如上面查......
  • ORM数据增删改查 django请求生命周期 django路由层 反向解析
    目录可视化界面之数据增删改查补充1.建表2.数据展示功能3.数据添加功能4.数据编辑功能5.数据删除功能django请求生命周期流程图crsfwsgirel与uwsginginxdjango路由层1.路......
  • 最大正方形问题
    最大正方形问题作者:Grey原文地址:博客园:最大正方形问题CSDN:最大正方形问题题目描述在一个由'0'和'1'组成的二维矩阵内,找到只包含'1'的最大正方形,并返回其面积......
  • [windows]远程桌面失败提示CredSSP加密修正
    前言windows远程桌面失败,提示“CredSSP加密……”远程桌面服务器的系统版本:WindowsServer2016本地电脑的系统版本:Windows10组策略管理器Win+R,输入“gpedit.m......
  • 实验七-缓冲区溢出
    实验七-缓冲区溢出1.实验指导书内容一、实验简介缓冲区溢出是指程序试图向缓冲区写入超出预分配固定长度数据的情况。这一漏洞可以被恶意用户利用来改变程序的流控制,......
  • QGis插件编写-GeocodeCN
     以前写的插件记录,我把它搬到博客园上来,代码已更新许多,以最新为准。该工具目的是地址转为坐标点(地理编码),刚开始只是一小段Python脚本,批量请求百度地图开放接口进行编码,......
  • MTK结构图
    @目录简介结构图简介MTK模块划分概述结构图编译说明编译环境搭建和编译方法文件说明工程以及源文件说明工具使用工具使用说明......
  • 阿里云安装Mono 发生错误解决方法
    园豆兑换阿里云代金券体验阿里云云服务器javascript:void(0)活动,花1块钱体验了一下阿里云,安装了CentOS6.2后,在其上按照《32和64位的CentOS6.0下安装......
  • 尝试用微博记录 SQL Server 2012开发者训练营笔记
    花了2天时间参加微软的SQLServer2012开发者训练营,全面的学习了SQLServer2012上面的新特性,尝试使用微博做笔记。现在把它摘录到博客,在做个整理,下面是......