pychram写,一个有对话框窗口 库存管理软件,可以入库和出库,查询库存,记录入库时间和出库时间,数据本地保存
时间: 2023-09-25 12:13:33 浏览: 71
kucunguanli.rar_ kucunguanli _入库_库存管理_库存管理 java_管理系统
5星 · 资源好评率100%
好的,你可以先安装PyQt5库(如果你还没有安装的话),它可以用来创建GUI界面。然后你需要创建一个主窗口,里面包含输入和输出框、查询、入库、出库等按钮。你可以使用QTableWidget来显示库存信息,并使用SQLite作为本地数据库进行数据存储。以下是一个简单的代码示例:
```python
import sys
import sqlite3
from PyQt5.QtCore import Qt, QDateTime
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QGridLayout, QLabel, QLineEdit, QPushButton, QMessageBox, QTableWidget, QTableWidgetItem
class InventoryManager(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('库存管理软件')
self.resize(800, 600)
# 数据库初始化
self.conn = sqlite3.connect('inventory.db')
self.c = self.conn.cursor()
self.c.execute('''CREATE TABLE IF NOT EXISTS inventory
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
quantity INTEGER NOT NULL,
in_time TEXT NOT NULL,
out_time TEXT)''')
self.conn.commit()
# 界面初始化
central_widget = QWidget(self)
self.setCentralWidget(central_widget)
vbox = QVBoxLayout(central_widget)
grid = QGridLayout()
vbox.addLayout(grid)
# 标签和文本框
grid.addWidget(QLabel('名称:'), 0, 0)
self.name_edit = QLineEdit()
grid.addWidget(self.name_edit, 0, 1)
grid.addWidget(QLabel('数量:'), 1, 0)
self.quantity_edit = QLineEdit()
grid.addWidget(self.quantity_edit, 1, 1)
# 查询按钮
query_button = QPushButton('查询')
query_button.clicked.connect(self.query_inventory)
grid.addWidget(query_button, 2, 0)
# 入库按钮
in_button = QPushButton('入库')
in_button.clicked.connect(self.in_inventory)
grid.addWidget(in_button, 2, 1)
# 出库按钮
out_button = QPushButton('出库')
out_button.clicked.connect(self.out_inventory)
grid.addWidget(out_button, 2, 2)
# 表格
self.table = QTableWidget()
self.table.setColumnCount(5)
self.table.setHorizontalHeaderLabels(['ID', '名称', '数量', '入库时间', '出库时间'])
vbox.addWidget(self.table)
def query_inventory(self):
self.table.setRowCount(0)
name = self.name_edit.text().strip()
self.c.execute('SELECT * FROM inventory WHERE name LIKE ?', ('%'+name+'%',))
data = self.c.fetchall()
for row in data:
self.table.insertRow(0)
for i, item in enumerate(row):
self.table.setItem(0, i, QTableWidgetItem(str(item)))
def in_inventory(self):
name = self.name_edit.text().strip()
quantity = int(self.quantity_edit.text().strip())
in_time = QDateTime.currentDateTime().toString(Qt.ISODate)
self.c.execute('INSERT INTO inventory (name, quantity, in_time) VALUES (?, ?, ?)', (name, quantity, in_time))
self.conn.commit()
QMessageBox.information(self, '提示', '入库成功!')
self.name_edit.clear()
self.quantity_edit.clear()
def out_inventory(self):
name = self.name_edit.text().strip()
quantity = int(self.quantity_edit.text().strip())
out_time = QDateTime.currentDateTime().toString(Qt.ISODate)
self.c.execute('SELECT id, quantity FROM inventory WHERE name=? AND quantity>=?', (name, quantity))
data = self.c.fetchall()
if len(data) == 0:
QMessageBox.warning(self, '错误', '库存不足!')
else:
for row in data:
if quantity == 0:
break
id, q = row
if q >= quantity:
self.c.execute('UPDATE inventory SET quantity=?, out_time=? WHERE id=?', (q-quantity, out_time, id))
quantity = 0
else:
self.c.execute('UPDATE inventory SET quantity=?, out_time=? WHERE id=?', (0, out_time, id))
quantity -= q
self.conn.commit()
QMessageBox.information(self, '提示', '出库成功!')
self.name_edit.clear()
self.quantity_edit.clear()
def closeEvent(self, event):
self.conn.close()
event.accept()
if __name__ == '__main__':
app = QApplication(sys.argv)
manager = InventoryManager()
manager.show()
sys.exit(app.exec_())
```
这个程序可以创建一个主窗口,里面包含查询、入库、出库按钮和一个表格。用户可以输入名称和数量,点击对应的按钮进行操作。数据会保存在SQLite数据库中,下次打开程序时可以继续使用。
阅读全文