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)
时间: 2024-03-28 17:40:01 浏览: 21
这段代码是用来显示 Excel 表格的数据的。首先,通过 Treeview 控件创建一个表格,然后设置滚动条并显示表格。接下来,设置表格列的标题和宽度,通过循环遍历列的标题来设置。最后,遍历 Excel 表格数据,将每行数据插入到表格中进行显示。需要注意的是,如果该行数据全为空值,则不会被插入到表格中进行显示。
相关问题
def show_excel(self): # 清空文本框内容 self.result_text.delete(1.0, tk.END) self.result_text2.delete(1.0, tk.END)# 显示第二个表格 header = next(self.data_sheet.iter_rows(min_row=1, max_row=1, values_only=True)) print(header) # 创建表格 Frame 和滚动条 table_frame2 = tk.Frame(self.result_text2) table_frame2.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) table_scroll_y2 = ttk.Scrollbar(table_frame2, orient=tk.VERTICAL) table_scroll_y2.pack(side=tk.RIGHT, fill=tk.Y) table_scroll_x2 = ttk.Scrollbar(table_frame2, orient=tk.HORIZONTAL) table_scroll_x2.pack(side=tk.BOTTOM, fill=tk.X) # 创建表格 table2 = ttk.Treeview(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)每次调用 show_excel 方法时,都会创建一个名为 table_frame 的表格 Frame。这可能会导致多次调用 show_excel 方法时创建多个表格 Frame。怎麽解決
可以在创建表格 Frame 之前,先判断该 Frame 是否已经存在,如果已经存在,则不需要再创建,直接使用已经存在的 Frame 即可。可以在初始化方法中创建一个成员变量 self.table_frame,然后在 show_excel 方法中判断该变量是否为 None,如果为 None,则创建表格 Frame,否则直接使用 self.table_frame。代码示例如下:
```python
def __init__(self):
...
self.table_frame = None
def show_excel(self):
...
if self.table_frame is None:
self.table_frame = tk.Frame(self.result_text2)
self.table_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
table_scroll_y2 = ttk.Scrollbar(self.table_frame, orient=tk.VERTICAL)
table_scroll_y2.pack(side=tk.RIGHT, fill=tk.Y)
table_scroll_x2 = ttk.Scrollbar(self.table_frame, orient=tk.HORIZONTAL)
table_scroll_x2.pack(side=tk.BOTTOM, fill=tk.X)
table2 = ttk.Treeview(self.table_frame, 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)
...
```
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) # 清空第二个表格内容 if hasattr(self, 'table_frame'): self.table_frame.destroy() self.table_frame = tk.Frame(self.result_text) self.table_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) table_scroll_y = ttk.Scrollbar(self.table_frame, orient=tk.VERTICAL) table_scroll_y.pack(side=tk.RIGHT, fill=tk.Y) table_scroll_x = ttk.Scrollbar(self.table_frame, orient=tk.HORIZONTAL) table_scroll_x.pack(side=tk.BOTTOM, fill=tk.X) # 显示第一个表格 header = next(self.record_sheet.iter_rows(min_row=1, max_row=1, values_only=True)) # 创建表格 table = ttk.Treeview(self.table_frame, columns=header, show='headings',
height=10, yscrollcommand=table_scroll_y.set, xscrollcommand=table_scroll_x.set)
# 设置表格样式
table.column("#0", width=0, stretch=tk.NO)
for i, col in enumerate(header):
table.column(col, width=100, stretch=tk.NO)
table.heading(col, text=col)
# 填充表格数据
for row in self.record_sheet.iter_rows(min_row=2, values_only=True):
table.insert("", tk.END, values=row)
# 显示第二个表格
header2 = next(self.record_sheet2.iter_rows(min_row=1, max_row=1, values_only=True)) # 创建表格
table2 = ttk.Treeview(self.table_frame2, columns=header2, show='headings', height=10, yscrollcommand=table_scroll_y2.set, xscrollcommand=table_scroll_x2.set)
# 设置表格样式
table2.column("#0", width=0, stretch=tk.NO)
for i, col in enumerate(header2):
table2.column(col, width=100, stretch=tk.NO)
table2.heading(col, text=col)
# 填充表格数据
for row in self.record_sheet2.iter_rows(min_row=2, values_only=True):
table2.insert("", tk.END, values=row)
# 显示表格
table.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
table_scroll_y.config(command=table.yview)
table_scroll_x.config(command=table.xview)
table2.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
table_scroll_y2.config(command=table2.yview)
table_scroll_x2.config(command=table2.xview)
self.result_text.config(state=tk.NORMAL)
self.result_text.insert(tk.END, "Excel表格读取完成!\n")
self.result_text.config(state=tk.DISABLED)
这段代码是用来读取 Excel 表格并在界面上显示的。其中,通过 Treeview 控件实现了表格的显示,使用 Scrollbar 实现了滚动条。同时,还支持同时显示两个表格。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)