def show_excel(self): style = ttk.Style() style.configure("MyTreeview1.Treeview", rowheight=25, borderwidth=2, 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) 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)
时间: 2024-03-15 08:47:31 浏览: 104
在你的代码中,第二个 Treeview 组件的样式名为 "MyTreeview2.Treeview",但在创建组件时没有指定该样式。
你需要确保在创建第二个 Treeview 组件时,将样式名设置为 "MyTreeview2.Treeview",例如:
```
treeview2 = ttk.Treeview(self.container1, columns=columns2, show="headings", style="MyTreeview2.Treeview")
```
这样就能够避免 "wrong # args" 的错误。
相关问题
def show_excel(self): style = ttk.Style() style.configure("MyTreeview1.Treeview", rowheight=25, borderwidth=2, 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)修改這個代碼顯示excel數據時不要將excel中公式顯示出來,只顯示最後的結果,請修改出最後完整代碼
您可以在代码中添加一个函数,使用`openpyxl`包中的`value`属性将公式转换为结果。以下是修改后的完整代码:
```python
import tkinter as tk
from tkinter import ttk
from openpyxl import load_workbook
from openpyxl.utils import get_column_letter
class ExcelViewer:
def __init__(self, file_path):
self.file_path = file_path
self.workbook = load_workbook(filename=file_path, read_only=True)
self.record_sheet = self.workbook.active
self.root = tk.Tk()
self.root.title("Excel Viewer")
self.container1 = ttk.Frame(self.root, padding=10)
self.container1.grid(row=0, column=0, sticky="nsew")
self.container1.columnconfigure(0, weight=1)
self.container1.rowconfigure(1, weight=1)
self.show_excel()
def show_excel(self):
style = ttk.Style()
style.configure("MyTreeview1.Treeview", rowheight=25, borderwidth=2, 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 = [self.get_cell_value(cell) for cell in row]
if all(not bool(cell) for cell in row_values):
continue
treeview1.insert("", tk.END, values=row_values)
def get_cell_value(self, cell):
if cell is None:
return ""
elif cell.data_type == "f":
return cell.value # 返回公式
elif cell.data_type == "n":
return round(cell.value, 2) # 返回数字类型的值
else:
return cell.value # 返回其他类型的值
def run(self):
self.root.mainloop()
if __name__ == "__main__":
ev = ExcelViewer("example.xlsx")
ev.run()
```
在 `get_cell_value` 函数中,我们首先检查单元格的数据类型,如果是 `f` (公式)类型,则返回公式本身。否则,如果是数字类型,则返回数字类型的值。对于其他类型的单元格,直接返回其值。这样,公式就会被转换为其结果并显示在表格中。
def show_excel(self): self.wb = openpyxl.load_workbook(r"C:\Users\bing3_chen\Desktop\1.xlsx") self.record_sheet = self.wb["記錄"] self.data_sheet = self.wb["數據"] style = ttk.Style() style.configure("MyTreeview1.Treeview", rowheight=25, borderwidth=2, 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 = [str(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) # 创建表格用于显示第二个工作表 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") 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 = [str(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)給這兩個表格框各自添加滾動條來滾動表格數據
您可以在创建表格时添加一个滚动条,并将表格放置在一个 Frame 中,这样就可以实现表格滚动了。以下是示例代码:
```
# 创建第一个表格的 Frame 和滚动条
frame1 = tk.Frame(self.container1)
frame1.grid(row=1, column=3, rowspan=1, padx=5, pady=5, sticky="nsew")
treeview1 = ttk.Treeview(frame1, columns=columns1, show="headings", style="MyTreeview1.Treeview")
treeview1.pack(side="left", fill="both", expand=True)
scrollbar1 = tk.Scrollbar(frame1, command=treeview1.yview)
scrollbar1.pack(side="right", fill="y")
treeview1.configure(yscrollcommand=scrollbar1.set)
# 创建第二个表格的 Frame 和滚动条
frame2 = tk.Frame(self.container1)
frame2.grid(row=3, column=3, padx=5, pady=5, sticky="nsew")
treeview2 = ttk.Treeview(frame2, columns=columns2, show="headings")
treeview2.pack(side="left", fill="both", expand=True)
scrollbar2 = tk.Scrollbar(frame2, command=treeview2.yview)
scrollbar2.pack(side="right", fill="y")
treeview2.configure(yscrollcommand=scrollbar2.set)
```
这样,您就可以在容器中创建两个带有滚动条的表格了。
阅读全文