python使用tkinter实现商品信息的增删改查
时间: 2023-09-10 08:15:24 浏览: 218
好的,下面是一个基于tkinter的商品信息增删改查的示例代码:
```python
from tkinter import *
import tkinter.messagebox as messagebox
class Goods:
def __init__(self, name, price, stock):
self.name = name
self.price = price
self.stock = stock
class GoodsManagement:
def __init__(self, master):
self.master = master
self.master.title("商品信息管理系统")
self.master.geometry("500x300")
# 商品名称
self.label_name = Label(self.master, text="商品名称:")
self.label_name.grid(row=0, column=0)
self.entry_name = Entry(self.master)
self.entry_name.grid(row=0, column=1)
# 商品价格
self.label_price = Label(self.master, text="商品价格:")
self.label_price.grid(row=1, column=0)
self.entry_price = Entry(self.master)
self.entry_price.grid(row=1, column=1)
# 商品库存
self.label_stock = Label(self.master, text="商品库存:")
self.label_stock.grid(row=2, column=0)
self.entry_stock = Entry(self.master)
self.entry_stock.grid(row=2, column=1)
# 操作按钮
self.btn_add = Button(self.master, text="添加商品", command=self.add_goods)
self.btn_add.grid(row=3, column=0)
self.btn_modify = Button(self.master, text="修改商品", command=self.modify_goods)
self.btn_modify.grid(row=3, column=1)
self.btn_delete = Button(self.master, text="删除商品", command=self.delete_goods)
self.btn_delete.grid(row=3, column=2)
self.btn_search = Button(self.master, text="查询商品", command=self.search_goods)
self.btn_search.grid(row=3, column=3)
# 商品列表
self.goods_list = Listbox(self.master)
self.goods_list.grid(row=4, columnspan=4, padx=5, pady=5)
# 初始化商品数据
self.goods_data = [
Goods("商品1", 10, 100),
Goods("商品2", 20, 200),
Goods("商品3", 30, 300)
]
for goods in self.goods_data:
self.goods_list.insert(END, goods.name)
# 添加商品
def add_goods(self):
name = self.entry_name.get().strip()
price = self.entry_price.get().strip()
stock = self.entry_stock.get().strip()
if name and price and stock:
new_goods = Goods(name, int(price), int(stock))
self.goods_data.append(new_goods)
self.goods_list.insert(END, new_goods.name)
messagebox.showinfo("提示", "添加商品成功")
else:
messagebox.showwarning("警告", "请填写完整商品信息")
# 修改商品
def modify_goods(self):
index = self.goods_list.curselection()
if index:
selected_goods = self.goods_data[index[0]]
name = self.entry_name.get().strip()
price = self.entry_price.get().strip()
stock = self.entry_stock.get().strip()
if name and price and stock:
selected_goods.name = name
selected_goods.price = int(price)
selected_goods.stock = int(stock)
self.goods_list.delete(index)
self.goods_list.insert(index, selected_goods.name)
messagebox.showinfo("提示", "修改商品成功")
else:
messagebox.showwarning("警告", "请填写完整商品信息")
else:
messagebox.showwarning("警告", "请选择要修改的商品")
# 删除商品
def delete_goods(self):
index = self.goods_list.curselection()
if index:
selected_goods = self.goods_data[index[0]]
self.goods_data.remove(selected_goods)
self.goods_list.delete(index)
self.entry_name.delete(0, END)
self.entry_price.delete(0, END)
self.entry_stock.delete(0, END)
messagebox.showinfo("提示", "删除商品成功")
else:
messagebox.showwarning("警告", "请选择要删除的商品")
# 查询商品
def search_goods(self):
name = self.entry_name.get().strip()
if name:
self.goods_list.selection_clear(0, END)
for i, goods in enumerate(self.goods_data):
if goods.name == name:
self.goods_list.selection_set(i)
self.entry_name.delete(0, END)
self.entry_price.delete(0, END)
self.entry_stock.delete(0, END)
self.entry_name.insert(END, goods.name)
self.entry_price.insert(END, goods.price)
self.entry_stock.insert(END, goods.stock)
messagebox.showinfo("提示", "查询商品成功")
return
messagebox.showwarning("警告", "未找到该商品")
else:
messagebox.showwarning("警告", "请输入要查询的商品名称")
if __name__ == '__main__':
root = Tk()
app = GoodsManagement(root)
root.mainloop()
```
该示例代码中,我们使用了一个自定义的Goods类表示商品信息,使用一个Listbox控件展示商品列表。在初始化时,我们添加了一些示例商品数据。当用户点击添加、修改、删除、查询按钮时,会调用对应的方法,并在方法中实现相应的操作。当用户选择某个商品时,我们会把该商品的信息显示在输入框中。需要注意的是,这里的添加、修改、删除、查询商品的代码需要根据具体的业务需求进行实现。
阅读全文