import tkinter as tk
from tkinter import ttk
def load_data():
filename = entry_filename.get()
data = []
with open(filename, 'r') as f:
for line in f:
items = line.strip().split('\t')
data.append(items)
return data
def on_button_click():
data = load_data()
tree.delete(*tree.get_children())
for i, row in enumerate(data, start=1):
tree.insert("", "end", text=str(i), values=row)
创建主窗口
window = tk.Tk()
创建标签和输入框
label_filename = tk.Label(window, text="文件名:")
label_filename.grid(row=0, column=0)
entry_filename = tk.Entry(window)
entry_filename.grid(row=0, column=1)
创建按钮
button = tk.Button(window, text="加载", command=on_button_click)
button.grid(row=1, columnspan=2)
创建表格和滚动条
tree_frame = ttk.Frame(window)
tree_frame.grid(row=2, columnspan=2)
tree = ttk.Treeview(tree_frame, columns=('col1', 'col2', 'col3', 'col4', 'col5'), show='headings')
tree.column('col1', width=100, anchor='center')
tree.heading('col1', text='列1')
tree.column('col2', width=100, anchor='center')
tree.heading('col2', text='列2')
tree.column('col3', width=100, anchor='center')
tree.heading('col3', text='列3')
tree.column('col4', width=100, anchor='center')
tree.heading('col4', text='列4')
tree.column('col5', width=100, anchor='center')
tree.heading('col5', text='列5')
创建滚动条
vsb = ttk.Scrollbar(tree_frame, orient="vertical", command=tree.yview)
tree.configure(yscrollcommand=vsb.set)
vsb.pack(side="right", fill="y")
tree.pack(side="left", fill="both", expand=True)
设置数据列
data = []
创建下一页按钮
button_next = tk.Button(window, text="下一页", state='disabled')
button_next.grid(row=3, columnspan=2)
运行主循环
window.mainloop()
上面是python原生库
这是第二个源码,这个很垃圾,卡的要死,但是据说是很强大的库
import tkinter as tk
from tkinter import ttk
import pandas as pd
import chardet
def load_data():
filename = entry_filename.get()
with open(filename, 'rb') as f:
result = chardet.detect(f.read())
data = pd.read_csv(filename, sep='\t', encoding=result['encoding'])
return data.values.tolist()
def on_button_click():
data = load_data()
tree.delete(*tree.get_children())
for i, row in enumerate(data, start=1):
tree.insert("", "end", text=str(i), values=row)
创建主窗口
window = tk.Tk()
创建标签和输入框
label_filename = tk.Label(window, text="文件名:")
label_filename.grid(row=0, column=0)
entry_filename = tk.Entry(window)
entry_filename.grid(row=0, column=1)
创建按钮
button = tk.Button(window, text="加载", command=on_button_click)
button.grid(row=1, columnspan=2)
创建表格和滚动条
tree_frame = ttk.Frame(window)
tree_frame.grid(row=2, columnspan=2)
tree = ttk.Treeview(tree_frame, columns=('col1', 'col2', 'col3', 'col4', 'col5'), show='headings')
tree.column('col1', width=100, anchor='center')
tree.heading('col1', text='列1')
tree.column('col2', width=100, anchor='center')
tree.heading('col2', text='列2')
tree.column('col3', width=100, anchor='center')
tree.heading('col3', text='列3')
tree.column('col4', width=100, anchor='center')
tree.heading('col4', text='列4')
tree.column('col5', width=100, anchor='center')
tree.heading('col5', text='列5')
创建滚动条
vsb = ttk.Scrollbar(tree_frame, orient="vertical", command=tree.yview)
tree.configure(yscrollcommand=vsb.set)
vsb.pack(side="right", fill="y")
tree.pack(side="left", fill="both", expand=True)
设置数据列
data = []
创建下一页按钮
button_next = tk.Button(window, text="下一页", state='disabled')
button_next.grid(row=3, columnspan=2)
运行主循环
window.mainloop()