pyqt5圖書管理系統--4、管理員頁面設計之淘汰書籍
阿新 • • 發佈:2022-12-12
本節分為兩個部分:管理員介面設計、淘汰書籍介面設計。
主要流程: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的書籍,刪除成功!
- 書籍資訊處理記錄表資訊記錄成功
待後續更新....