# 查询按钮的回调函数 def query(self): # 检查日期是否填写 if not self.date_entry.get(): messagebox.showerror("Error", "未填寫日期.") return # 清空查询结果 for widget in self.result_text.winfo_children(): widget.destroy() # 获取所有查询条件 date = self.date_entry.get() name = self.line_entry1.get() name1 = self.line_entry2.get() # 查询数据 ws = openpyxl.load_workbook(r'D:\點檢系統存放資料夾\點檢明細\1.xlsx').active rows = ws.iter_rows(min_row=1, values_only=True) records = [] header = next(rows) for row in rows: if (not date or row[2] == date) and (not name or row[5] == name) and (not name1 or row[8] == name1): records.append(row) # 显示查询结果 table_frame = tk.Frame(self.result_text) table = ttk.Treeview(table_frame, columns=header, show='headings') table.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) # 设置表格列标题 for col in header: table.heading(col, text=col) table.column(col, width=120) # 调整间隔距离 for row in records: table.insert('', tk.END, values=row) # 创建一个垂直滚动条并将其与表格关联 scrollbar = ttk.Scrollbar(table_frame, orient=tk.VERTICAL, command=table.yview) table.configure(yscrollcommand=scrollbar.set) scrollbar.pack(side=tk.RIGHT, fill=tk.Y) # 将表格和滚动条添加到一个框架中 table.pack(in_=table_frame, side=tk.LEFT, fill=tk.BOTH, expand=True) table_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) scrollbar.pack(in_=table_frame, side=tk.RIGHT, fill=tk.Y) download_button = ttk.Button(self.result_text, text="下載", command=lambda: self.download_excel(records)) download_button.pack(side=tk.BOTTOM)將這個代碼中下載按鈕滾動條的前面
时间: 2023-12-04 21:02:49 浏览: 117
Python RuntimeError: thread.__init__() not called解决方法
5星 · 资源好评率100%
可以尝试将下载按钮的创建代码放在滚动条之前,这样下载按钮就会出现在表格上方。修改后的代码如下:
```python
# 显示查询结果
table_frame = tk.Frame(self.result_text)
table = ttk.Treeview(table_frame, columns=header, show='headings')
table.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# 设置表格列标题
for col in header:
table.heading(col, text=col)
table.column(col, width=120) # 调整间隔距离
for row in records:
table.insert('', tk.END, values=row)
download_button = ttk.Button(self.result_text, text="下載", command=lambda: self.download_excel(records))
download_button.pack(side=tk.BOTTOM)
# 创建一个垂直滚动条并将其与表格关联
scrollbar = ttk.Scrollbar(table_frame, orient=tk.VERTICAL, command=table.yview)
table.configure(yscrollcommand=scrollbar.set)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# 将表格和滚动条添加到一个框架中
table.pack(in_=table_frame, side=tk.LEFT, fill=tk.BOTH, expand=True)
table_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
scrollbar.pack(in_=table_frame, side=tk.RIGHT, fill=tk.Y)
```
阅读全文