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 浏览: 102
在你的代码中,第二个 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 = [] for cell in row: if cell is None: row_values.append("") elif cell.data_type == 'f': row_values.append(cell.value) else: row_values.append(cell.value) 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 = [] for cell in row: if cell is None: row_values.append("") elif cell.data_type == 'f': row_values.append(cell.value) else: row_values.append(cell.value) if all(not bool(cell) for cell in row_values): continue treeview2.insert("", tk.END, values=row_values)
这段代码是用于显示 Excel 文件中的两个工作表内容的,其中使用了 ttk.Treeview 控件来实现表格的显示。第一个工作表的列标题和内容通过 self.record_sheet 来获取,第二个工作表的列标题和内容通过 self.data_sheet 来获取。同时,代码还设置了表格列的宽度和对齐方式。如果某一行的所有单元格都是空的,就不会被显示出来。
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)修改創建表格框的大小
要修改表格框的大小,可以在创建表格时设置其尺寸。具体来说,可以在调用 `ttk.Treeview` 函数时,通过 `height` 和 `width` 参数来设置表格框的高度和宽度,例如:
```
treeview1 = ttk.Treeview(self.container1, columns=columns1, show="headings", style="MyTreeview1.Treeview", height=10, width=50)
```
上述代码将创建一个高度为 10,宽度为 50 的表格框。同理,可以通过修改第二个表格的创建代码来改变其大小。
阅读全文