class InventoryApp: def __init__(self, master): self.master = master master.title("物料进出库统计") self.master.state('zoomed') # 窗口最大化self.label12 = ttk.Label(self.container_top, text="數量:") self.label12.grid(row=1, column=0, padx=5, pady=5) self.material_qty12 = ttk.Combobox(self.container_top, values=[]) self.material_qty12.grid(row=1, column=1, padx=5, pady=5)self.label6 = ttk.Label(self.container_top, text="品名:") self.label6.grid(row=5, column=0, padx=5, pady=5) self.material_qty6 = ttk.Combobox(self.container_top, values=[]) self.material_qty6.grid(row=5, column=1, padx=5, pady=5)修改這個代碼,獲取第一個下拉輸入框中寫入的值,在打開的excel工作表名為數據的表匹配A列相應數據的第4列數據填寫到第2個下拉輸入框中
时间: 2024-03-10 12:45:19 浏览: 58
Python-Lectures-master.rar_Master Class_Python learning
你可以在代码中添加以下函数来实现这个功能:
```
import openpyxl
class InventoryApp:
def __init__(self, master):
self.master = master
master.title("物料进出库统计")
master.state('zoomed')
self.label12 = ttk.Label(self.container_top, text="數量:")
self.label12.grid(row=1, column=0, padx=5, pady=5)
self.material_qty12 = ttk.Combobox(self.container_top, values=[])
self.material_qty12.grid(row=1, column=1, padx=5, pady=5)
self.label6 = ttk.Label(self.container_top, text="品名:")
self.label6.grid(row=5, column=0, padx=5, pady=5)
self.material_qty6 = ttk.Combobox(self.container_top, values=[])
self.material_qty6.grid(row=5, column=1, padx=5, pady=5)
def read_excel(self, file_path, sheet_name):
wb = openpyxl.load_workbook(file_path)
ws = wb[sheet_name]
data = []
for row in ws.iter_rows(values_only=True):
data.append(row[0])
return data
def get_material_qty(self):
material_qty_value = self.material_qty12.get()
file_path = 'your_file_path.xlsx' # 更改为你的文件路径和名称
sheet_name = '数据' # 更改为你的工作表名称
data = self.read_excel(file_path, sheet_name)
for row in range(1, len(data)):
if data[row] == material_qty_value:
self.material_qty6.set(ws.cell(row=row+1, column=4).value)
break
```
然后在你的代码中调用此函数,如下所示:
```
self.material_qty12.bind("<<ComboboxSelected>>", lambda event: self.get_material_qty())
```
这将在选择第一个下拉框中的值时调用 `get_material_qty()` 函数,该函数将从 Excel 文件中读取数据并将其填充到第二个下拉框中。
阅读全文