赞
踩
锋哥原创的PyQt6图书管理系统视频教程:
- """
- 图书实体类
- 作者 : 小锋老师
- 官网 : www.python222.com
- """
-
-
- # 图书类
- class Book:
- # 编号 主键ID
- id = None
- # 图书名称
- bookName = None
- # 图书作者
- author = None
- # 作者性别
- sex = '男'
- # 图书价格
- price = None
- # 图书类别ID
- bookTypeId = -1
- # 图书类别名称
- bookTypeName = None
- # 图书类别描述
- bookDesc = None
-
- def __init__(self, bookName, author, bookTypeId):
- self.bookName = bookName
- self.author = author
- self.bookTypeId = bookTypeId
-
- @staticmethod
- def my_constructor(bookName, author, sex, price, bookTypeId, bookDesc):
- obj = Book(bookName, author, bookTypeId)
- obj.sex = sex
- obj.price = price
- obj.bookDesc = bookDesc
- return obj
bookDao.py里新建add方法:
- def add(book: Book):
- """
- 图书添加
- :param bookType: 图书实体
- :return: 返回执行的记录条数
- """
- con = None
- try:
- con = dbUtil.getCon()
- cursor = con.cursor()
- cursor.execute(
- f"insert into t_book values(null,'{book.bookName}','{book.author}','{book.sex}',{book.price},{book.bookTypeId},'{book.bookDesc}')")
- return cursor.rowcount
- except Exception as e:
- print(e)
- con.rollback()
- return 0
- finally:
- dbUtil.closeCon(con)
bookAdd.py的Ui_Form里,新建add方法:
- def add(self):
- """
- 添加图书
- :return:
- """
- bookName = self.bookNameInput.text()
- if bookName.strip() == "":
- QMessageBox.warning(None, '系统提示', '图书名称不能为空!')
- return
- author = self.authorInput.text()
- if author.strip() == "":
- QMessageBox.warning(None, '系统提示', '作者不能为空!')
- return
- sex = ""
- if self.manRadio.isChecked():
- sex = "男"
- else:
- sex = "女"
- price = self.priceInput.text()
- if price.strip() == "":
- QMessageBox.warning(None, '系统提示', '价格不能为空!')
- return
- bookTypeId = self.bookTypeComboBox.currentData()
- if bookTypeId == -1:
- QMessageBox.warning(None, '系统提示', '请选择图书类别!')
- return
- bookDesc = self.bookDescInput.toPlainText()
- book = Book.my_constructor(bookName, author, sex, price, bookTypeId, bookDesc)
- if bookDao.add(book) > 0:
- QMessageBox.information(None, '系统提示', '添加成功!')
- self.reset()
- else:
- QMessageBox.warning(None, '系统提示', '添加失败!')
再加个重置表单方法:
- def reset(self):
- """
- 重置表单
- :return:
- """
- self.bookNameInput.setText("")
- self.authorInput.setText("")
- self.manRadio.setChecked(True)
- self.priceInput.setText("")
- self.bookTypeComboBox.setCurrentIndex(0)
- self.bookDescInput.setPlainText("")
图书添加UI模块,我们让男默认选中
- # 设置默认选中
- self.manRadio.setChecked(True)
绑定添加按钮事件
- # 添加按钮事件
- self.addBtn.clicked.connect(self.add)
运行测试:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。