def show_excel(self): # 清空文本框内容 self.result_text.delete(1.0, tk.END) self.result_text2.delete(1.0, tk.END) # 显示出入库明细 for row in self.record_sheet.iter_rows(values_only=True): row_str = "" for cell in row: if cell is None: cell = "" elif type(cell) is datetime.datetime: cell = cell.strftime("%Y-%m-%d") row_str += str(cell) + "\t" self.result_text.insert(tk.END, row_str + "\n") # 显示库存明细 for row in self.data_sheet.iter_rows(values_only=True): row_str = "" for cell in row: if cell is None: cell = "" elif type(cell) is datetime.datetime: cell = cell.strftime("%Y-%m-%d") row_str += str(cell) + "\t" self.result_text2.insert(tk.END, row_str + "\n")修改這段代碼顯示數據需要修改為表格數據,列於列之間要有寬度
时间: 2024-03-23 08:38:25 浏览: 43
要将数据显示为表格形式,可以考虑使用tkinter中的表格控件,例如tkinter.ttk.Treeview。下面是一个示例代码,可以将数据显示为表格形式,并设置列宽。
```python
import tkinter as tk
from tkinter import ttk
class App(tk.Tk):
def __init__(self, data):
super().__init__()
self.title("Data Table")
# 设置表格
columns = ["Column 1", "Column 2", "Column 3"]
self.table = ttk.Treeview(self, columns=columns, show="headings")
self.table.heading("Column 1", text="Column 1")
self.table.heading("Column 2", text="Column 2")
self.table.heading("Column 3", text="Column 3")
# 设置列宽
self.table.column("Column 1", width=100)
self.table.column("Column 2", width=100)
self.table.column("Column 3", width=100)
# 添加数据
for row in data:
self.table.insert("", tk.END, values=row)
# 显示表格
self.table.pack(expand=True, fill=tk.BOTH)
data = [
("1", "2", "3"),
("4", "5", "6"),
("7", "8", "9")
]
app = App(data)
app.mainloop()
```
你可以将上面的代码嵌入到你的程序中,然后将数据转换为列表形式,传递给App类的构造函数。例如,你可以将以下代码替换为你的show_excel函数:
```python
def show_excel(self):
# 清空文本框内容
self.result_text.delete(1.0, tk.END)
self.result_text2.delete(1.0, tk.END)
# 显示出入库明细
data1 = []
for row in self.record_sheet.iter_rows(values_only=True):
data1.append(row)
if data1:
app1 = App(data1)
app1.title("Record Sheet")
app1.mainloop()
# 显示库存明细
data2 = []
for row in self.data_sheet.iter_rows(values_only=True):
data2.append(row)
if data2:
app2 = App(data2)
app2.title("Data Sheet")
app2.mainloop()
```
这样就可以以表格形式显示数据,并设置列宽了。
阅读全文