import tkinter as tk import pandas as pd from tkinter import messagebox # 读取Excel表格数据 df = pd.read_excel(r'C:\Users\bing3_chen\Desktop\1.xlsx', sheet_name='總表') # 创建GUI窗口 window = tk.Tk() window.title('物料进出库管理') window.geometry('400x200') # 进货函数 def add_inventory(): # 获取物料名称和数量 name = name_entry.get() quantity = int(quantity_entry.get()) # 查找相同名称的数据并进行加操作 group = df.groupby('名稱').sum() if name in group.index: group.loc[name, '數量'] += quantity else: group.loc[name] = [quantity] group.reset_index(inplace=True) # 将修改后的数据写回Excel表格 group.to_excel(r'C:\Users\bing3_chen\Desktop\1.xlsx', index=False, sheet_name='總表', mode='a', header=False) # 清空文本框 name_entry.delete(0, tk.END) quantity_entry.delete(0, tk.END) # 出货函数 def remove_inventory(): # 获取物料名称和数量 name = name_entry.get() quantity = int(quantity_entry.get()) # 查找相同名称的数据并进行减操作 group = df.groupby('名稱').sum() if name in group.index and group.loc[name, '數量'] >= quantity: group.loc[name, '數量'] -= quantity else: messagebox.showerror('Error', '物料不足!') return group.reset_index(inplace=True) # 将修改后的数据写回Excel表格 group.to_excel(r'C:\Users\bing3_chen\Desktop\1.xlsx', index=False, sheet_name='總表', mode='a', header=False) # 清空文本框 name_entry.delete(0, tk.END) quantity_entry.delete(0, tk.END) # 添加控件 tk.Label(window, text='物料名称:').grid(row=0, column=0) name_entry = tk.Entry(window) name_entry.grid(row=0, column=1) tk.Label(window, text='物料数量:').grid(row=1, column=0) quantity_entry = tk.Entry(window) quantity_entry.grid(row=1, column=1) tk.Button(window, text='进货', command=add_inventory).grid(row=2, column=0) tk.Button(window, text='出货', command=remove_inventory).grid(row=2, column=1) window.mainloop()將這個代碼中pabds庫可以修改成openpyxl庫嗎
时间: 2024-03-05 22:48:55 浏览: 32
可以的,只需要将`import pandas as pd`改成`from openpyxl import load_workbook`,然后将读取Excel表格和写回Excel表格的部分修改成openpyxl库的方式即可。修改后的代码如下:
```
import tkinter as tk
from openpyxl import load_workbook
from tkinter import messagebox
# 读取Excel表格数据
wb = load_workbook(filename=r'C:\Users\bing3_chen\Desktop\1.xlsx')
ws = wb['總表']
# 创建GUI窗口
window = tk.Tk()
window.title('物料进出库管理')
window.geometry('400x200')
# 进货函数
def add_inventory():
# 获取物料名称和数量
name = name_entry.get()
quantity = int(quantity_entry.get())
# 查找相同名称的数据并进行加操作
found = False
for row in ws.iter_rows(min_row=2, values_only=True):
if row[0] == name:
row[1] += quantity
found = True
break
if not found:
ws.append([name, quantity])
# 将修改后的数据写回Excel表格
wb.save(filename=r'C:\Users\bing3_chen\Desktop\1.xlsx')
# 清空文本框
name_entry.delete(0, tk.END)
quantity_entry.delete(0, tk.END)
# 出货函数
def remove_inventory():
# 获取物料名称和数量
name = name_entry.get()
quantity = int(quantity_entry.get())
# 查找相同名称的数据并进行减操作
found = False
for row in ws.iter_rows(min_row=2, values_only=True):
if row[0] == name:
if row[1] >= quantity:
row[1] -= quantity
found = True
else:
messagebox.showerror('Error', '物料不足!')
return
if not found:
messagebox.showerror('Error', '物料不存在!')
return
# 将修改后的数据写回Excel表格
wb.save(filename=r'C:\Users\bing3_chen\Desktop\1.xlsx')
# 清空文本框
name_entry.delete(0, tk.END)
quantity_entry.delete(0, tk.END)
# 添加控件
tk.Label(window, text='物料名称:').grid(row=0, column=0)
name_entry = tk.Entry(window)
name_entry.grid(row=0, column=1)
tk.Label(window, text='物料数量:').grid(row=1, column=0)
quantity_entry = tk.Entry(window)
quantity_entry.grid(row=1, column=1)
tk.Button(window, text='进货', command=add_inventory).grid(row=2, column=0)
tk.Button(window, text='出货', command=remove_inventory).grid(row=2, column=1)
window.mainloop()
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)