锋哥原创的PyQt6图书管理系统视频教程:
PyQt6图书管理系统视频教程 Python桌面开发 Python入门级项目实战 (无废话版) 火爆连载更新中~_哔哩哔哩_bilibiliPyQt6图书管理系统视频教程 Python桌面开发 Python入门级项目实战 (无废话版) 火爆连载更新中~共计24条视频,包括:PyQt6图书管理系统视频教程 Python桌面开发 Python入门级项目实战 (无废话版) 火爆连载更新中~、第2讲 登录功能UI设计实现、第3讲 数据库操作工具包dbUtil.py封装等,UP主更多精彩视频,请关注UP账号。https://www.bilibili.com/video/BV18t4y1R7Qp/首先搜索模块,图书类别下拉框初始化下
定义initBookTypeListComboBox方法
def initBookTypeListComboBox(self):
"""
初始化下拉框数据
:return:
"""
bookTypeList = bookTypeDao.list("") # 获取所有图书类别信息
self.s_bookTypeComboBox.addItem("请选择图书类别...", -1)
for bookType in bookTypeList:
self.s_bookTypeComboBox.addItem(bookType[1], bookType[0])
构造方法里调用
bookDao里定义list方法:
def list(s_book: Book):
"""
根据条件查询图书信息
:return:返回查询的图书信息列表
"""
con = None
try:
con = dbUtil.getCon()
cursor = con.cursor()
sql = "SELECT t.id AS id,bookName,author,bookTypeName,sex,price,bookDesc FROM t_book AS t ,t_booktype AS tk WHERE t.`bookTypeId`=tk.`id`"
if s_book != None:
if s_book.bookName.strip() != '':
sql += " and t.bookName like '%" + s_book.bookName + "%'"
if s_book.author.strip() != '':
sql += " and t.author like '%" + s_book.author + "%'"
if s_book.bookTypeId != -1:
sql += " and t.bookTypeId = " + str(s_book.bookTypeId)
print(sql)
cursor.execute(sql)
return cursor.fetchall()
except Exception as e:
print(e)
con.rollback()
return None
finally:
dbUtil.closeCon(con)
bookManage.py里再定义initTable方法
def initTable(self):
"""
根据条件初始化数据表格
:return:
"""
bookName = self.s_nameInput.text()
author = self.s_authorInput.text()
bookTypeId = self.s_bookTypeComboBox.currentData()
s_book = Book(bookName, author, bookTypeId)
result = bookDao.list(s_book)
row = 0
if result:
row = len(result)
self.bookTable.setColumnCount(7)
self.bookTable.setRowCount(row)
self.bookTable.verticalHeader().setVisible(False) # 隐藏垂直标题 序号
self.bookTable.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers) # 禁止编辑单元格
self.bookTable.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
self.bookTable.setHorizontalHeaderLabels(
['编号', '图书名称', '图书作者', '图书类别', '作者性别', '图书价格', '图书描述'])
self.bookTable.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Stretch) # 列头自适应
self.bookTable.setSelectionBehavior(
QAbstractItemView.SelectionBehavior.SelectRows) # 设置选择行为,以行为单位
for i in range(row):
for j in range(7):
data = QTableWidgetItem(str(result[i][j]))
self.bookTable.setItem(i, j, data)
构造方法里调用
搜索按钮绑定事件
# 搜索按钮点击事件
self.s_searchBtn.clicked.connect(self.initTable)
测试: