pyqt5图书管理系统--3、管理员页面设计之添加书籍
本节主要分为两个部分;一个为管理员界面设计,另一个为添加书籍界面。
主要流程:1、通过进入管理员界面,设置三个按钮(添加书籍、用户管理、淘汰书籍);
2、当管理员点击添加书籍,即转到添加书籍界面;
3、添加书籍界面显示出可操作的标签输入框和按钮,进行添加相应书籍信息;
4、对比添加的信息进行相应设置和提示;
5、连接数据库,将所获取的输入框文本内容添加进数据库,并更新添加书籍的相应信息。
一、管理员界面
- 模块:
import sys
from PyQt5.QtWidgets import *
import qdarkstyle
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtSql import *
from addBookDialog import addBookDialog
(一)类基础
class AdminHome(QWidget):
def __init__(self):
super(AdminHome, self).__init__()
self.setUpUi()
(二)页面布局
1、界面基础布局
self.resize(900,600)
self.setWindowTitle("欢迎进入管理员主页")
self.layout = QHBoxLayout()
self.buttonlayout = QVBoxLayout()
self.setLayout(self.layout)
2、添加标签
self.userManageButton = QPushButton("用户管理")
self.addBookButton = QPushButton("添加书籍")
self.dropBookButton = QPushButton("淘汰书籍")
3、设置字体
font = QFont()
font.setPixelSize(16)
self.userManageButton.setFont(font)
self.addBookButton.setFont(font)
self.dropBookButton.setFont(font)
4、设置宽高
self.userManageButton.setFixedWidth(100)
self.userManageButton.setFixedHeight(42)
self.addBookButton.setFixedWidth(100)
self.addBookButton.setFixedHeight(42)
self.dropBookButton.setFixedWidth(100)
self.dropBookButton.setFixedHeight(42)
5、添加至垂直布局
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)
- 通过发送的信号调用模块函数。
from addBookDialog import addBookDialog
- 启动新窗口调用添加书籍界面对话框。
def addBookButtonClicked(self):
addDialog = addBookDialog()
addDialog.show()
addDialog.exec_()
二、添加书籍界面
- 模块:
import sys
from PyQt5.QtWidgets import *
import qdarkstyle
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtSql import *
import time
(一)类基础设置
class addBookDialog(QDialog):
接受信号
add_book_success_sign = pyqtSignal()
def __init__(self):
super(addBookDialog,self).__init__()
self.setWindowModality(Qt.WindowModal)
self.setWindowTitle("添加书籍")
self.setUpUI()
(二)添加书籍界面布局
1、页面大小布局
self.resize(300,400)
self.layout = QFormLayout()
self.setLayout(self.layout)
2、添加书籍的标签及大小
(1)添加书籍标签
self.titleLabel = QLabel("添加书籍")
self.bookNameLabel = QLabel("书 名:")
self.bookIdLabel = QLabel("书 号:")
self.authNameLabel = QLabel("作 者:")
self.categoryLabel = QLabel("分 类:")
self.publisherLabel = QLabel("出 版 社:")
self.publisherDateLabel = QLabel("出版日期:")
self.addNameLabel = QLabel("数 量:")
# 添加的按钮标签
self.addBookButton = 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.addNameLabel.setFont(font)
(3)设置按钮大小
# 设置button
font.setPixelSize(16)
self.addBookButton.setFont(font)
self.addBookButton.setFixedHeight(32)
self.addBookButton.setFixedWidth(140)
3、添加相对应的输入框
- 选项输入框类型
BookCategory = ["哲学","教育","生物学","社会科学","政治", "法律","军事","经济","文化","体育","语言文字","地理","天文学","医疗卫生","农业"]
# lineEdit控件
self.bookNameEdit = QLineEdit()
self.bookIdEdit = QLineEdit()
self.authNameEdit = QLineEdit()
# 分类创建的QComboBox,存储整个分类的列表
# QComboBox 以占用最少屏幕控件的方式向用户呈现选项列表的方法
self.categoryEdit = QComboBox()
self.categoryEdit.addItems(BookCategory)
self.publisherEdit = QLineEdit()
# QDateTime 时间控件
# setDisplayFormat 设置显示的格式 yyyy-MM-dd 年-月-日
self.publisherTime = QDateTimeEdit()
self.publisherTime.setDisplayFormat("yyyy-MM-dd")
self.addNumEdit = QLineEdit()
4、设置输入框输入的方法和规则
(1)输入框长度设置
# 限制输入的长度
self.bookNameEdit.setMaxLength(10)
self.bookIdEdit.setMaxLength(6)
self.authNameEdit.setMaxLength(10)
self.publisherEdit.setMaxLength(10)
self.addNumEdit.setMaxLength(12)
self.addNumEdit.setValidator(QIntValidator())
(2)输入框间距设置
# 标题间距
self.titleLabel.setMargin(8)
# 输入框垂直布局上下行之间的间距
self.layout.setVerticalSpacing(10)
(三)连接数据库
1、发送信号
-
点击添加按钮进行信号触发。
-
信号触发后调用函数。
self.addBookButton.clicked.connect(self.addBookButtonCicked)
2、获取信号
-
在连接数据库函数内,接受信号,进行信号处理。
(1)获取输入框内容
bookName = self.bookNameEdit.text()
bookId = self.bookIdEdit.text()
authName = self.authNameEdit.text()
bookCategory = self.categoryEdit.currentText()
publisher = self.publisherEdit.text()
publisherTime = self.publisherTime.text()
addBookNum = self.addNumEdit.text()
(2)逻辑判断
-
若获取输入框文本内容时,发现输入框为空时,进行提醒。
-
若不为空则执行else语句
if (bookName == "" or bookId == "" or authName=="" or bookCategory==""
or publisher=="" or publisherTime=="" or addBookNum==""):
print(QMessageBox.warning(self, "警告", "输入框不可为空!",QMessageBox.Yes, QMessageBox.Yes))
-
else语句:连接数据库
# 将添加的书籍数量转为整数类型
addBookNum = int(addBookNum)
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)
-
若书号bookId存在则直接添加库存量和可借阅量的值。
if (query.next()):
# 添加到库存量和可借阅量
sql = "update Book set NumStorage=NumStorage+%d,NumCanBorrow +%d where BooId='%s'" %(addBookNum)
-
若书号bookId不存在则创建新的书名和书号及相关数据
else:
sql = "insert into Book values ('%s','%s','%s','%s','%s','%s',%d,%d,0)" % (
bookName, bookId, authName, bookCategory, publisher, publisherTime, addBookNum, addBookNum)
query.exec_(sql)
db.commit()
-
同时,打开图书添加信息表
-
将添加的图书书号、添加时的时间、添加书籍的数量进行更新插入
timenow = time.strftime('%Y-%m-%d', time.localtime(time.time()))
sql = "insert into buyordrop values ('%s', '%s', 1, '%d')" % (bookId, timenow, addBookNum)
query.exec_(sql)
db.commit()
-
最后,提交添加书籍后的信号,关闭并清空输入框。
self.add_book_success_sign.emit()
self.close()
self.clearEdit()
def clearEdit(self):
self.bookNameEdit.clear()
self.bookIdEdit.clear()
self.authNameEdit.clear()
self.addNumEdit.clear()
self.publisherEdit.clear()
(四)程序运行入口
if __name__ == '__main__':
app = QApplication(sys.argv)
am = addBookDialog()
am.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
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)
def addBookButtonClicked(self):
addDialog = addBookDialog()
addDialog.show()
addDialog.exec_()
if __name__ == '__main__':
app = QApplication(sys.argv)
mianMindow = AdminHome()
mianMindow.show()
sys.exit(app.exec_())
(二)添加书籍的完整代码
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import time
"""
QDialog
对话框
"""
class addBookDialog(QDialog):
add_book_success_sign = pyqtSignal()
def __init__(self):
super(addBookDialog,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)
# 添加书籍的标签
self.titleLabel = QLabel("添加书籍")
self.bookNameLabel = QLabel("书 名:")
self.bookIdLabel = QLabel("书 号:")
self.authNameLabel = QLabel("作 者:")
self.categoryLabel = QLabel("分 类:")
self.publisherLabel = QLabel("出 版 社:")
self.publisherDateLabel = QLabel("出版日期:")
self.addNameLabel = QLabel("数 量:")
# button 控件
self.addBookButton = QPushButton("添加")
# lineEdit控件
self.bookNameEdit = QLineEdit()
self.bookIdEdit = QLineEdit()
self.authNameEdit = QLineEdit()
# 分类创建的QComboBox,存储整个分类的列表
# QComboBox 以占用最少屏幕控件的方式向用户呈现选项列表的方法
self.categoryEdit = QComboBox()
self.categoryEdit.addItems(BookCategory)
self.publisherEdit = QLineEdit()
# QDateTime 时间控件
# setDisplayFormat 设置显示的格式 yyyy-MM-dd 年-月-日
self.publisherTime = QDateTimeEdit()
self.publisherTime.setDisplayFormat("yyyy-MM-dd")
self.addNumEdit = QLineEdit()
# 限制输入的长度
self.bookNameEdit.setMaxLength(10)
self.bookIdEdit.setMaxLength(6)
self.authNameEdit.setMaxLength(10)
self.publisherEdit.setMaxLength(10)
self.addNumEdit.setMaxLength(12)
self.addNumEdit.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.publisherTime)
self.layout.addRow(self.addNameLabel,self.addNumEdit)
self.layout.addRow(self.addBookButton)
# 标签字体大小设置
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.addNameLabel.setFont(font)
# 设置button
font.setPixelSize(16)
self.addBookButton.setFont(font)
self.addBookButton.setFixedHeight(32)
self.addBookButton.setFixedWidth(140)
# 设置间距
self.titleLabel.setMargin(8)
# 垂直布局上下行之间的间距
self.layout.setVerticalSpacing(10)
self.addBookButton.clicked.connect(self.addBookButtonCicked)
def addBookButtonCicked(self):
bookName = self.bookNameEdit.text()
bookId = self.bookIdEdit.text()
authName = self.authNameEdit.text()
bookCategory = self.categoryEdit.currentText()
publisher = self.publisherEdit.text()
publisherTime = self.publisherTime.text()
addBookNum = self.addNumEdit.text()
if (bookName == "" or bookId == "" or authName=="" or bookCategory==""
or publisher=="" or publisherTime=="" or addBookNum==""):
print(QMessageBox.warning(self, "警告", "输入框不可为空!",QMessageBox.Yes, QMessageBox.Yes))
else:
addBookNum = int(addBookNum)
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName('./db/LibraryManagement.db')
db.open()
query = QSqlQuery()
# 如果已存在,则更新Book的现存量,剩余可借量,不存在,则添加到book表,
# 同时更新采购表 采购1 淘汰0
sql = "select * from Book where BookID='%s'" % (bookId)
query.exec_(sql)
if (query.next()):
# 添加到库存量和可借阅量
sql = "update Book set NumStorage=NumStorage+%d,NumCanBorrow +%d where BooId='%s'" %(addBookNum)
else:
sql = "insert into Book values ('%s','%s','%s','%s','%s','%s',%d,%d,0)" % (
bookName, bookId, authName, bookCategory, publisher, publisherTime, addBookNum, addBookNum)
query.exec_(sql)
db.commit()
timenow = time.strftime('%Y-%m-%d', time.localtime(time.time()))
sql = "insert into buyordrop values ('%s', '%s', 1, '%d')" % (bookId, timenow, addBookNum)
query.exec_(sql)
db.commit()
print(QMessageBox.information(self, "提示", "添加书籍成功!",QMessageBox.Yes,QMessageBox.Yes))
self.add_book_success_sign.emit()
self.close()
self.clearEdit()
return
# 清空
def clearEdit(self):
self.bookNameEdit.clear()
self.bookIdEdit.clear()
self.authNameEdit.clear()
self.addNumEdit.clear()
self.publisherEdit.clear()
if __name__ == '__main__':
app = QApplication(sys.argv)
am = addBookDialog()
am.show()
sys.exit(app.exec_())
四、效果展示
(一)管理员页面效果展示
初始布局有点丑,待后续进行界面优化......
(二)添加书籍页面展示
1、点击添加书籍
2、添加书籍信息
3、提交信息
4、数据库书籍信息
5、书籍书号、添加时间及数量的修改。
待后续更新....