在pyqt中,创建一个界面,实现商品数据库的增删改查功能,显示选择商品的信息,商品信息来自于labelimg数据集,给出完整代码
时间: 2024-04-01 14:31:26 浏览: 90
以下是基于PyQt5实现商品数据库的增删改查功能,并显示选择商品的信息的完整代码示例:
```python
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
import sqlite3
class ProductInfo(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 创建控件
self.name_label = QLabel('商品名称')
self.price_label = QLabel('商品价格')
self.intro_label = QLabel('商品介绍')
self.name_edit = QLineEdit()
self.price_edit = QLineEdit()
self.intro_edit = QTextEdit()
self.add_button = QPushButton('添加')
self.delete_button = QPushButton('删除')
self.update_button = QPushButton('更新')
self.search_button = QPushButton('查询')
self.table = QTableWidget()
self.table.setColumnCount(3)
self.table.setHorizontalHeaderLabels(['名称', '价格', '介绍'])
self.table.setSelectionBehavior(QAbstractItemView.SelectRows)
# 设置布局
hbox1 = QHBoxLayout()
hbox1.addWidget(self.name_label)
hbox1.addWidget(self.name_edit)
hbox1.addWidget(self.price_label)
hbox1.addWidget(self.price_edit)
hbox2 = QHBoxLayout()
hbox2.addWidget(self.intro_label)
hbox2.addWidget(self.intro_edit)
vbox1 = QVBoxLayout()
vbox1.addLayout(hbox1)
vbox1.addLayout(hbox2)
vbox1.addWidget(self.add_button)
vbox1.addWidget(self.delete_button)
vbox1.addWidget(self.update_button)
vbox1.addWidget(self.search_button)
vbox2 = QVBoxLayout()
vbox2.addWidget(self.table)
hbox3 = QHBoxLayout()
hbox3.addLayout(vbox1)
hbox3.addLayout(vbox2)
self.setLayout(hbox3)
# 设置信号槽
self.add_button.clicked.connect(self.add_product)
self.delete_button.clicked.connect(self.delete_product)
self.update_button.clicked.connect(self.update_product)
self.search_button.clicked.connect(self.search_product)
self.table.cellClicked.connect(self.show_product)
# 创建数据库
self.conn = sqlite3.connect('product.db')
self.cursor = self.conn.cursor()
self.cursor.execute('''CREATE TABLE IF NOT EXISTS product (
name TEXT PRIMARY KEY,
price REAL,
intro TEXT
)''')
self.conn.commit()
self.load_data()
def load_data(self):
# 加载商品数据
self.table.setRowCount(0)
self.cursor.execute('SELECT * FROM product')
data = self.cursor.fetchall()
for row in data:
row_index = self.table.rowCount()
self.table.insertRow(row_index)
self.table.setItem(row_index, 0, QTableWidgetItem(row[0]))
self.table.setItem(row_index, 1, QTableWidgetItem(str(row[1])))
self.table.setItem(row_index, 2, QTableWidgetItem(row[2]))
def add_product(self):
# 添加商品信息
name = self.name_edit.text()
price = self.price_edit.text()
intro = self.intro_edit.toPlainText()
if name and price and intro:
try:
self.cursor.execute('INSERT INTO product VALUES (?, ?, ?)', (name, price, intro))
self.conn.commit()
self.load_data()
QMessageBox.information(self, '信息', '添加成功')
except:
QMessageBox.warning(self, '警告', '添加失败')
else:
QMessageBox.warning(self, '警告', '请填写完整商品信息')
def delete_product(self):
# 删除商品信息
row = self.table.currentRow()
if row >= 0:
name = self.table.item(row, 0).text()
reply = QMessageBox.question(self, '确认', '您确定要删除该商品吗?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
self.cursor.execute('DELETE FROM product WHERE name = ?', (name,))
self.conn.commit()
self.load_data()
QMessageBox.information(self, '信息', '删除成功')
else:
QMessageBox.warning(self, '警告', '请选择要删除的商品')
def update_product(self):
# 更新商品信息
row = self.table.currentRow()
if row >= 0:
name = self.table.item(row, 0).text()
price = self.price_edit.text()
intro = self.intro_edit.toPlainText()
if price and intro:
try:
self.cursor.execute('UPDATE product SET price = ?, intro = ? WHERE name = ?', (price, intro, name))
self.conn.commit()
self.load_data()
QMessageBox.information(self, '信息', '更新成功')
except:
QMessageBox.warning(self, '警告', '更新失败')
else:
QMessageBox.warning(self, '警告', '请填写完整商品信息')
else:
QMessageBox.warning(self, '警告', '请选择要更新的商品')
def search_product(self):
# 查询商品信息
name = self.name_edit.text()
self.table.setRowCount(0)
if name:
self.cursor.execute('SELECT * FROM product WHERE name LIKE ?', ('%' + name + '%',))
else:
self.cursor.execute('SELECT * FROM product')
data = self.cursor.fetchall()
for row in data:
row_index = self.table.rowCount()
self.table.insertRow(row_index)
self.table.setItem(row_index, 0, QTableWidgetItem(row[0]))
self.table.setItem(row_index, 1, QTableWidgetItem(str(row[1])))
self.table.setItem(row_index, 2, QTableWidgetItem(row[2]))
def show_product(self, row, col):
# 显示商品信息
name = self.table.item(row, 0).text()
price = self.table.item(row, 1).text()
intro = self.table.item(row, 2).text()
self.name_edit.setText(name)
self.price_edit.setText(price)
self.intro_edit.setText(intro)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = ProductInfo()
win.show()
sys.exit(app.exec_())
```
这个示例程序中,我们使用了PyQt5的一些常用控件,如QLabel、QLineEdit、QTextEdit、QPushButton和QTableWidget等。我们还利用了sqlite3库来创建和管理商品数据库,并实现了数据库的增删改查功能。
阅读全文