库存管理是任何处理实物商品的企业的一个重要方面。Python 提供了各种库来读取和写入文件,使其成为管理库存的绝佳选择。文件处理是一个强大的工具,它允许我们使用 Python 等编程语言来操作计算机文件系统上的文件。在本文中,我们将探讨如何使用文件处理在 Tkinter 中实现库存管理系统。
使用 Tkinter 的库存管理系统项目
将新产品添加到库存
此函数将新的库存条目添加到文本文件中。它从输入字段检索项目名称和数量,以追加模式打开文件,并写入 item_name、item_qty。然后它会清除输入字段。
def add_inventory():
item_name = item_name_entry.get()
item_qty = int(item_qty_entry.get())
with open('inventory.txt', 'a') as file:
file.write(f'{item_name},{item_qty}\n')
item_name_entry.delete(0, tk.END)
item_qty_entry.delete(0, tk.END)
更新库存
此函数 update_inventory() 根据给定的输入更新值更新现有的 item_name 和 item_qty 。它以写入模式打开文件并从当前清单中读取数据。它循环遍历每一行,查找与 item_name 的匹配项,如果找到则更新记录。
def update_inventory():
item_name = item_name_entry.get()
item_qty = int(item_qty_entry.get())
with open('inventory.txt', 'r') as file:
inventory_data = file.readlines()
with open('inventory.txt', 'w') as file:
for line in inventory_data:
name, qty = line.strip().split(',')
if name == item_name:
file.write(f'{name},{item_qty}\n')
else:
file.write(line)
item_name_entry.delete(0, tk.END)
item_qty_entry.delete(0, tk.END)
搜索并显示当前库存
该函数从需要搜索的输入字段中提取 item_name 。当以只读模式打开时,它会迭代 inventory.txt 文件的每一行。在这里,如果商品名称与搜索名称匹配,我们将使用 result_label 显示商品名称和金额。
def search_inventory():
search_name = item_name_entry.get()
with open('inventory.txt', 'r') as file:
for line in file:
name, qty = line.strip().split(',')
if name == search_name:
result_label.config(text=f'{name} - {qty}')
return
result_label.config(text=f'{search_name} not found in inventory.')
item_name_entry.delete(0, tk.END)
从库存中移除物品
remove_inventory() 函数从 inventory.txt 文件中删除特定的库存条目。该函数检索要从输入字段中删除的项目名称。它以读取模式打开 inventory.txt 文件,并将所有库存数据读入列表中。然后以写入模式再次打开该文件。它会迭代库存数据中的每一行,检查商品名称是否与要删除的商品名称匹配,然后跳过写入该行。
def remove_inventory():
remove_name = item_name_entry.get()
with open('inventory.txt', 'r') as file:
inventory_data = file.readlines()
with open('inventory.txt', 'w') as file:
for line in inventory_data:
name, qty = line.strip().split(',')
if name != remove_name:
file.write(line)
item_name_entry.delete(0, tk.END)
item_qty_entry.delete(0, tk.END)
生成完整的库存清单
generate_inventory () 函数从文本文件生成库存条目的完整列表。它读取文件并将库存数据存储在列表中。然后,库存数据将连接成带有换行符的字符串。最后,该字符串显示在result_label中以显示完整的库存列表。
def generate_inventory():
with open('inventory.txt', 'r') as file:
inventory_data = file.readlines()
inventory_text = '\n'.join(inventory_data)
result_label.config(text=inventory_text)
完整代码
import tkinter as tk
# 添加新库存分录的功能
def add_inventory():
item_name = item_name_entry.get()
item_qty = int(item_qty_entry.get())
with open('inventory.txt', 'a') as file:
file.write(f'{item_name},{item_qty}\n')
item_name_entry.delete(0, tk.END)
item_qty_entry.delete(0, tk.END)
# 更新现有库存分录的功能
def update_inventory():
item_name = item_name_entry.get()
item_qty = int(item_qty_entry.get())
with open('inventory.txt', 'r') as file:
inventory_data = file.readlines()
with open('inventory.txt', 'w') as file:
for line in inventory_data:
name, qty = line.strip().split(',')
if name == item_name:
file.write(f'{name},{item_qty}\n')
else:
file.write(line)
item_name_entry.delete(0, tk.END)
item_qty_entry.delete(0, tk.END)
# 用于搜索和显示库存条目的功能
def search_inventory():
search_name = item_name_entry.get()
with open('inventory.txt', 'r') as file:
for line in file:
name, qty = line.strip().split(',')
if name == search_name:
result_label.config(text=f'{name} - {qty}')
return
result_label.config(text=f'{search_name} not found in inventory.')
item_name_entry.delete(0, tk.END)
# 删除现有库存分录的功能
def remove_inventory():
remove_name = item_name_entry.get()
with open('inventory.txt', 'r') as file:
inventory_data = file.readlines()
with open('inventory.txt', 'w') as file:
for line in inventory_data:
name, qty = line.strip().split(',')
if name != remove_name:
file.write(line)
item_name_entry.delete(0, tk.END)
item_qty_entry.delete(0, tk.END)
# 用于生成完整库存列表的函数
def generate_inventory():
with open('inventory.txt', 'r') as file:
inventory_data = file.readlines()
inventory_text = '\n'.join(inventory_data)
result_label.config(text=inventory_text)
# 创建主窗口
root = tk.Tk()
root.title("Inventory Management")
# 输入字段
item_name_label = tk.Label(root, text="Item Name:")
item_name_label.grid(row=0, column=0, padx=5, pady=5)
item_name_entry = tk.Entry(root)
item_name_entry.grid(row=0, column=1, padx=5, pady=5)
item_qty_label = tk.Label(root, text="Item Quantity:")
item_qty_label.grid(row=1, column=0, padx=5, pady=5)
item_qty_entry = tk.Entry(root)
item_qty_entry.grid(row=1, column=1, padx=5, pady=5)
# 创建按钮
add_button = tk.Button(root, text="Add Inventory",
command=add_inventory)
add_button.grid(row=2, column=0, padx=5, pady=5)
update_button = tk.Button(root, text="Update Inventory",
command=update_inventory)
update_button.grid(row=2, column=1, padx=5, pady=5)
search_button = tk.Button(root, text="Search Inventory",
command=search_inventory)
search_button.grid(row=3, column=0, padx=5, pady=5)
remove_button = tk.Button(root, text="Remove Inventory",
command=remove_inventory)
remove_button.grid(row=3, column=1, padx=5, pady=5)
generate_button = tk.Button(root, text="Generate Inventory",
command=generate_inventory)
generate_button.grid(row=4, column=0, padx=5, pady=5)
result_label = tk.Label(root, text="List")
result_label.grid(row=5, column=0, padx=5, pady=5)
root.mainloop()