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.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) for i in range(2): dyu = self.duibi()
时间: 2024-04-01 22:37:24 浏览: 24
这段代码展示了第二个表格,它首先创建了一个表格 Frame 和滚动条,然后读取 Excel 表格的第一行作为表格的标题,创建一个 Treeview 对象作为表格,设置表格的列标题和宽度,最后按行读取 Excel 表格的数据并插入到表格中展示出来。接下来它循环了两次调用了一个名为 `duibi()` 的函数。
相关问题
將上面代碼修改為這種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)
如果您想要使用 Treeview 来显示表格,可以将以下代码段:
```
columns1 = next(self.record_sheet.iter_rows(min_row=1, max_row=1, values_only=True))
print(columns1)
# 输出表格列的标题
self.result_text.insert(tk.END, "\t".join(columns1) + "\n")
# 显示第一个工作表的内容
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
# 输出表格内容
self.result_text.insert(tk.END, "\t".join(row_values) + "\n")
```
改为以下代码:
```
header = next(self.record_sheet.iter_rows(min_row=1, max_row=1, values_only=True))
print(header)
table_frame = tk.Frame(self.result_text)
table_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
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=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)
```
这样会创建一个包含 Treeview 的框架 `table_frame`,并将其放置在 `self.result_text` 文本框中。然后,将表格列的标题和宽度设置为 Treeview 中的列,接着遍历所有行并将它们插入到 Treeview 中。最终,您将会得到一个显示表格数据的 Treeview。
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 实现了滚动条。同时,还支持同时显示两个表格。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)