修改筛选部分,要求自动生成厂家信息选择筛选
时间: 2023-10-03 19:11:01 浏览: 59
好的,您可以使用以下代码来修改筛选部分,实现自动生成厂家信息选择筛选的功能:
```python
import tkinter as tk
import openpyxl
# 读取 Excel 文件
wb = openpyxl.load_workbook('备件信息.xlsx')
sheet = wb.active
# 获取所有厂家信息
manufacturers = set()
for row in sheet.iter_rows(min_row=2, values_only=True):
manufacturers.add(row[1])
# 创建窗口
window = tk.Tk()
window.title('备件信息')
window.geometry('800x500')
# 创建表格
table = tk.Label(window, text='')
table.pack()
# 遍历每一行,找出当前库存小于安全库存的备件信息
data = []
for row in sheet.iter_rows(min_row=2, values_only=True):
if row[5] < row[6]:
data.append(row)
# 显示表格
table_text = '物品名称\t厂家\t规格型号\t计量单位\t存放地点\t当前库存\t安全库存\t备注\n'
for row in data:
table_text += f'{row[0]}\t{row[1]}\t{row[2]}\t{row[3]}\t{row[4]}\t{row[5]}\t{row[6]}\t{row[7]}\n'
table.config(text=table_text)
# 添加筛选功能
def filter_table():
filter_data = []
keyword = keyword_entry.get()
manufacturer = manufacturer_var.get()
for row in data:
if keyword in str(row) and (manufacturer == '全部' or row[1] == manufacturer):
filter_data.append(row)
table_text = '物品名称\t厂家\t规格型号\t计量单位\t存放地点\t当前库存\t安全库存\t备注\n'
for row in filter_data:
table_text += f'{row[0]}\t{row[1]}\t{row[2]}\t{row[3]}\t{row[4]}\t{row[5]}\t{row[6]}\t{row[7]}\n'
table.config(text=table_text)
keyword_label = tk.Label(window, text='关键字:')
keyword_label.pack(side='left')
keyword_entry = tk.Entry(window)
keyword_entry.pack(side='left')
manufacturer_label = tk.Label(window, text='厂家:')
manufacturer_label.pack(side='left')
manufacturer_var = tk.StringVar()
manufacturer_var.set('全部')
manufacturer_option = tk.OptionMenu(window, manufacturer_var, '全部', *manufacturers)
manufacturer_option.pack(side='left')
filter_button = tk.Button(window, text='筛选', command=filter_table)
filter_button.pack(side='left')
window.mainloop()
```
运行以上代码后,会弹出一个窗口,包含筛选功能和厂家信息选择筛选功能。您可以选择厂家信息,然后输入关键字进行筛选,也可以选择“全部”来进行全局筛选。
阅读全文