這兩段代碼是一個函數,儅用戶第一次運行這個函數,顯示正常,第二次運行時會在第二個文本框中再一次的創建一個表格,該怎麽修改
时间: 2024-03-23 08:40:18 浏览: 28
你可以在每次运行该函数之前,先判断第二个表格的 Frame 是否已经存在,如果存在则不再创建,而是清空表格中的内容,更新数据。修改后的代码如下:
```
def show_excel(self):
# 清空第一个表格内容
self.result_text.delete(1.0, tk.END)
# 清空第二个表格内容
if hasattr(self, 'table_frame2'):
self.table_frame2.destroy()
self.table_frame2 = tk.Frame(self.result_text2)
self.table_frame2.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
table_scroll_y2 = ttk.Scrollbar(self.table_frame2, orient=tk.VERTICAL)
table_scroll_y2.pack(side=tk.RIGHT, fill=tk.Y)
table_scroll_x2 = ttk.Scrollbar(self.table_frame2, orient=tk.HORIZONTAL)
table_scroll_x2.pack(side=tk.BOTTOM, fill=tk.X)
# 显示第一个表格
header = next(self.record_sheet.iter_rows(min_row=1, max_row=1, values_only=True))
print(header)
# 创建表格 Frame 和滚动条
table_frame = tk.Frame(self.result_text)
table_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
table_scroll_y = ttk.Scrollbar(table_frame, orient=tk.VERTICAL)
table_scroll_y.pack(side=tk.RIGHT, fill=tk.Y)
table_scroll_x = ttk.Scrollbar(table_frame, orient=tk.HORIZONTAL)
table_scroll_x.pack(side=tk.BOTTOM, fill=tk.X)
# 创建表格
table = ttk.Treeview(table_frame, columns=header, show='headings', yscrollcommand=table_scroll_y.set,
xscrollcommand=table_scroll_x.set)
table.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
table_scroll_y.config(command=table.yview)
table_scroll_x.config(command=table.xview)
# 设置表格列的标题和宽度
for col in header:
table.heading(col, text=col)
table.column(col, width=80, anchor="center")
# 显示第一个表格的内容
for row in self.record_sheet.iter_rows(min_row=2, values_only=True):
row_values = [str(cell) if cell is not None else "" for cell in row]
if all(not bool(cell) for cell in row_values):
continue
table.insert("", tk.END, values=row_values)
# 显示第二个表格
header = next(self.data_sheet.iter_rows(min_row=1, max_row=1, values_only=True))
print(header)
# 创建表格
table2 = ttk.Treeview(self.table_frame2, columns=header, show='headings', yscrollcommand=table_scroll_y2.set,
xscrollcommand=table_scroll_x2.set)
table2.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
table_scroll_y2.config(command=table2.yview)
table_scroll_x2.config(command=table2.xview)
# 设置表格列的标题和宽度
for col in header:
table2.heading(col, text=col)
table2.column(col, width=80, anchor="center")
# 显示第二个表格的内容
for row in self.data_sheet.iter_rows(min_row=2, values_only=True):
row_values = [str(cell) if cell is not None else "" for cell in row]
if all(not bool(cell) for cell in row_values):
continue
table2.insert("", tk.END, values=row_values)
```
这样,第二次运行时就会清空表格中的内容,并更新数据,而不会再次创建一个新的表格。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.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)