的步骤如下:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("文件内容展示")
tree = ttk.Treeview(root)
tree["columns"] = ("line", "content")
tree.column("#0", width=50)
tree.column("line", width=50)
tree.column("content", width=200)
tree.heading("#0", text="行号")
tree.heading("line", text="行号")
tree.heading("content", text="内容")
tree.pack(fill="both", expand=True)
def read_file(filename):
with open(filename, "r") as file:
for line_num, line_content in enumerate(file, start=1):
line_content = line_content.strip()
line_parts = line_content.split("\t")
tree.insert("", "end", text=str(line_num), values=(str(line_num), line_content))
filename = "path/to/your/file.txt" # 替换为实际的文件路径
read_file(filename)
root.mainloop()
这样,就可以逐行读取文件,用制表符分隔,并将每行内容插入到tkinter的Treeview控件中展示出来。
注意:以上代码仅提供了一个基本的示例,实际应用中可能需要根据具体需求进行适当的修改和扩展。
领取专属 10元无门槛券
手把手带您无忧上云