ttk.Treeview 宽度修改
时间: 2024-05-11 07:20:25 浏览: 204
要修改 ttk.Treeview 的宽度,可以使用 `column` 方法来设置列的宽度。以下是一个简单的例子:
```python
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
treeview = ttk.Treeview(root)
treeview.pack()
# 添加列
treeview["columns"] = ("1", "2")
# 设置列的宽度
treeview.column("#0", width=100)
treeview.column("1", width=200)
treeview.column("2", width=300)
# 添加数据
treeview.insert("", "end", text="Row 1", values=("Value 1", "Value 2"))
treeview.insert("", "end", text="Row 2", values=("Value 3", "Value 4"))
root.mainloop()
```
在这个例子中,我们使用 `column` 方法设置了三列的宽度,分别是 `#0`(Treeview 默认的列),`1` 和 `2`。你可以根据需要修改这些列的宽度。
相关问题
將上面代碼修改為這種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): style = ttk.Style() style.configure("MyTreeview1.Treeview", rowheight=25, borderwidth=1, relief="solid", font=('Arial', 10)) style.configure("MyTreeview1.Treeview.Heading", font=('Arial', 10, 'bold')) style.layout("MyTreeview1.Treeview", [('MyTreeview1.Treeview.treearea', {'sticky': 'nswe'})]) # 创建表格用于显示第一个工作表 columns1 = next(self.record_sheet.iter_rows(min_row=1, max_row=1, values_only=True)) treeview1 = ttk.Treeview(self.container1, columns=columns1, show="headings", style="MyTreeview1.Treeview") treeview1.grid(row=1, column=3, rowspan=1, padx=5, pady=5, sticky="nsew") # 设置表格列的标题和宽度 for col in columns1: treeview1.heading(col, text=col) treeview1.column(col, width=100, anchor="center") # 显示第一个工作表的内容 for row in self.record_sheet.iter_rows(min_row=2, values_only=True): row_values = [cell if cell is not None else "" for cell in row] if all(not bool(cell) for cell in row_values): continue treeview1.insert("", tk.END, values=row_values) # 创建表格用于显示第二个工作表 style.configure("MyTreeview2.Treeview", rowheight=25, borderwidth=1, relief="solid", font=('Arial', 10)) style.configure("MyTreeview2.Treeview.Heading", font=('Arial', 10, 'bold')) style.layout("MyTreeview2.Treeview", [('MyTreeview2.Treeview.treearea', {'sticky': 'nswe'})]) columns2 = next(self.data_sheet.iter_rows(min_row=1, max_row=1, values_only=True)) treeview2 = ttk.Treeview(self.container1, columns=columns2, show="headings", style="MyTreeview2.Treeview") treeview2.grid(row=3, column=3, padx=5, pady=5, sticky="nsew") # 设置表格列的标题和宽度 for col in columns2: treeview2.heading(col, text=col) treeview2.column(col, width=100, anchor="center") # 显示第二个工作表的内容 for row in self.data_sheet.iter_rows(min_row=2, values_only=True): row_values = [cell if cell is not None else "" for cell in row] if all(not bool(cell) for cell in row_values): continue treeview2.insert("", tk.END, values=row_values) # 设置文本框大小一致 self.material_name1.config(width=20) self.material_qty.config(width=20)修改這段代碼在表格中顯示的數據用實綫將兩個表格數據都匡起來的完整修改代碼
以下是修改后的代码,将两个表格数据都用实线框起来,并将表格标题字体加粗:
```python
def show_excel(self):
style = ttk.Style()
# 设置第一个表格样式
style.configure("MyTreeview1.Treeview", rowheight=25, borderwidth=1, relief="solid", font=('Arial', 10))
style.configure("MyTreeview1.Treeview.Heading", font=('Arial', 10, 'bold'))
style.layout("MyTreeview1.Treeview", [('MyTreeview1.Treeview.treearea', {'sticky': 'nswe'})])
# 创建第一个表格用于显示第一个工作表
columns1 = next(self.record_sheet.iter_rows(min_row=1, max_row=1, values_only=True))
treeview1 = ttk.Treeview(self.container1, columns=columns1, show="headings", style="MyTreeview1.Treeview")
treeview1.grid(row=1, column=3, rowspan=1, padx=5, pady=5, sticky="nsew")
# 设置第一个表格列的标题和宽度
for col in columns1:
treeview1.heading(col, text=col)
treeview1.column(col, width=100, anchor="center")
# 显示第一个工作表的内容
for row in self.record_sheet.iter_rows(min_row=2, values_only=True):
row_values = [cell if cell is not None else "" for cell in row]
if all(not bool(cell) for cell in row_values):
continue
treeview1.insert("", tk.END, values=row_values)
# 设置第二个表格样式
style.configure("MyTreeview2.Treeview", rowheight=25, borderwidth=1, relief="solid", font=('Arial', 10))
style.configure("MyTreeview2.Treeview.Heading", font=('Arial', 10, 'bold'))
style.layout("MyTreeview2.Treeview", [('MyTreeview2.Treeview.treearea', {'sticky': 'nswe'})])
# 创建第二个表格用于显示第二个工作表
columns2 = next(self.data_sheet.iter_rows(min_row=1, max_row=1, values_only=True))
treeview2 = ttk.Treeview(self.container1, columns=columns2, show="headings", style="MyTreeview2.Treeview")
treeview2.grid(row=3, column=3, padx=5, pady=5, sticky="nsew")
# 设置第二个表格列的标题和宽度
for col in columns2:
treeview2.heading(col, text=col)
treeview2.column(col, width=100, anchor="center")
# 显示第二个工作表的内容
for row in self.data_sheet.iter_rows(min_row=2, values_only=True):
row_values = [cell if cell is not None else "" for cell in row]
if all(not bool(cell) for cell in row_values):
continue
treeview2.insert("", tk.END, values=row_values)
# 用实线框起两个表格
ttk.Separator(self.container1, orient="horizontal").grid(row=2, column=3, sticky="ew", pady=5)
ttk.Separator(self.container1, orient="vertical").grid(row=1, column=2, rowspan=3, sticky="ns", padx=5)
# 设置文本框大小一致
self.material_name1.config(width=20)
self.material_qty.config(width=20)
```
阅读全文