pychram写,一个有对话框窗口 库存管理软件,可以入库(记录时间)和出库(记录时间),查询库存,核算入库成本,数据本地保存,输入数据为空时不处理
时间: 2023-09-20 07:11:51 浏览: 40
好的,我可以为您提供一些代码示例,您可以在此基础上进行开发。首先,您需要使用Tkinter库创建一个对话框窗口,然后使用SQLite或者其他数据库保存数据。
以下是一个简单的例子,可以帮助您入库和出库,查询库存和核算成本:
```python
import tkinter as tk
import sqlite3
class StockManagement:
def __init__(self, master):
self.master = master
master.title("库存管理软件")
# 创建输入框和标签
self.label1 = tk.Label(master, text="产品名称")
self.label1.grid(row=0, column=0)
self.label2 = tk.Label(master, text="数量")
self.label2.grid(row=1, column=0)
self.label3 = tk.Label(master, text="单价")
self.label3.grid(row=2, column=0)
self.entry1 = tk.Entry(master)
self.entry1.grid(row=0, column=1)
self.entry2 = tk.Entry(master)
self.entry2.grid(row=1, column=1)
self.entry3 = tk.Entry(master)
self.entry3.grid(row=2, column=1)
# 创建按钮
self.button1 = tk.Button(master, text="入库", command=self.add_stock)
self.button1.grid(row=3, column=0)
self.button2 = tk.Button(master, text="出库", command=self.remove_stock)
self.button2.grid(row=3, column=1)
self.button3 = tk.Button(master, text="查询库存", command=self.view_stock)
self.button3.grid(row=4, column=0)
self.button4 = tk.Button(master, text="核算成本", command=self.calculate_cost)
self.button4.grid(row=4, column=1)
def add_stock(self):
# 获取输入框中的内容
name = self.entry1.get()
quantity = self.entry2.get()
price = self.entry3.get()
# 判断是否有空的输入框
if not name or not quantity or not price:
return
# 连接数据库
conn = sqlite3.connect('stock.db')
c = conn.cursor()
# 在数据库中创建一个表
c.execute('''CREATE TABLE IF NOT EXISTS stock
(name text, quantity integer, price real, date text, type text)''')
# 将数据插入表中
c.execute("INSERT INTO stock VALUES (?, ?, ?, datetime(), ?)", (name, quantity, price, '入库'))
# 提交更改并关闭连接
conn.commit()
conn.close()
def remove_stock(self):
# 获取输入框中的内容
name = self.entry1.get()
quantity = self.entry2.get()
price = self.entry3.get()
# 判断是否有空的输入框
if not name or not quantity or not price:
return
# 连接数据库
conn = sqlite3.connect('stock.db')
c = conn.cursor()
# 在数据库中创建一个表
c.execute('''CREATE TABLE IF NOT EXISTS stock
(name text, quantity integer, price real, date text, type text)''')
# 将数据插入表中
c.execute("INSERT INTO stock VALUES (?, ?, ?, datetime(), ?)", (name, -quantity, price, '出库'))
# 提交更改并关闭连接
conn.commit()
conn.close()
def view_stock(self):
# 连接数据库
conn = sqlite3.connect('stock.db')
c = conn.cursor()
# 查询表中的所有数据
result = c.execute("SELECT name, SUM(quantity) FROM stock GROUP BY name")
# 创建一个新窗口来显示查询结果
top = tk.Toplevel(self.master)
# 显示查询结果
for row in result:
label = tk.Label(top, text="{}: {}".format(row[0], row[1]))
label.pack()
# 关闭连接
conn.close()
def calculate_cost(self):
# 连接数据库
conn = sqlite3.connect('stock.db')
c = conn.cursor()
# 查询表中的所有数据
result = c.execute("SELECT name, SUM(quantity), AVG(price) FROM stock GROUP BY name")
# 创建一个新窗口来显示查询结果
top = tk.Toplevel(self.master)
# 显示查询结果
for row in result:
label = tk.Label(top, text="{}: 总成本为{}元,平均成本为{}元/件".format(row[0], row[1]*row[2], row[2]))
label.pack()
# 关闭连接
conn.close()
root = tk.Tk()
app = StockManagement(root)
root.mainloop()
```
这个例子创建了一个简单的GUI窗口,您可以在其中输入产品名称、数量和单价,并且可以点击“入库”或“出库”按钮将数据保存到SQLite数据库中。您还可以点击“查询库存”或“核算成本”按钮来查询库存或计算成本。注意,这个例子还没有处理错误情况,您需要进一步完善代码。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)