import pandas as pd
import tkinter.ttk as ttk
import tkinter as tk
def readcvs():
df= pd.read_csv(r'C:\Users\renro001\Desktop\DEMO.txt',sep='\t')
print(df)
# ##标题栏
name = tuple(df.columns.tolist())
# print(name)
ac = [str(i) for i in range(len(name))]
# print(ac)
# 返回数据
data = tuple(map(tuple, df.values))
# print(data)
# 返回行数
a = df.shape[0]
# print(a)
return name, data, a, ac
def readcvs_chaxun(x=''):
df= pd.read_csv(r'C:\Users\renro001\Desktop\DEMO.txt',sep='\t')
print(df)
if x:
df = df[df['A'].astype(str).str.contains(x)]
# ##标题栏
name = tuple(df.columns.tolist())
# print(name)
ac = [str(i) for i in range(len(name))]
# print(ac)
# 返回数据
data = tuple(map(tuple, df.values))
# print(data)
# 返回行数
a = df.shape[0]
# print(a)
return name, data, a, ac
win = tk.Tk()
win.title('xxx')
win.geometry("1200x600+400+20")
def all_show():
name,data,a,ac = readcvs()
tv = ttk.Treeview(win, columns=name, show='headings', height=150)
ybar = tk.Scrollbar(win, orient=tk.VERTICAL,
command=tv.yview)
xbar = tk.Scrollbar(win, orient=tk.HORIZONTAL,
command=tv.xview)
tv.configure(yscroll=ybar.set)
tv.configure(xscroll=xbar.set)
ybar.pack(side=tk.RIGHT, fill=tk.Y)
xbar.pack(side=tk.BOTTOM, fill=tk.X)
tv.pack(fill='x', padx=10, pady=10)
for i in range(len(name)):
if i == 1:
tv.column(ac[i], width=20, anchor='w')
elif i == 3:
tv.column(ac[i], width=20, anchor='w')
else:
tv.column(ac[i], width=100, anchor='c')
tv.heading(ac[i], text=name[i])
for i in range(a):
tv.insert('', 'end', values=data[i])
cv1 = tk.StringVar()
x=cv1.get()
def chaxun_show():
global x
name,data,a,ac = readcvs_chaxun(x=cv1.get())
tv = ttk.Treeview(win, columns=name, show='headings', height=150)
ybar = tk.Scrollbar(win, orient=tk.VERTICAL,
command=tv.yview)
xbar = tk.Scrollbar(win, orient=tk.HORIZONTAL,
command=tv.xview)
tv.configure(yscroll=ybar.set)
tv.configure(xscroll=xbar.set)
ybar.pack(side=tk.RIGHT, fill=tk.Y)
xbar.pack(side=tk.BOTTOM, fill=tk.X)
tv.pack(fill='x', padx=10, pady=10)
for i in range(len(name)):
if i == 1:
tv.column(ac[i], width=20, anchor='w')
elif i == 3:
tv.column(ac[i], width=20, anchor='w')
else:
tv.column(ac[i], width=100, anchor='c')
tv.heading(ac[i], text=name[i])
for i in range(a):
tv.insert('', 'end', values=data[i])
b2 = tk.Button(win, text='显示所有', width=10, font=("等线", 16), bg='grey', bd=5, fg='white',
command=lambda :all_show()).pack(fill='x', padx=10, pady=10)
label3 = tk.Label(win, text='按A列数据筛选:', font=("等线", 16)).pack(fill='x', padx=10, pady=10)
entry1 = tk.Entry(win, font=("等线", 16),textvariable=cv1).pack(fill='x', padx=10, pady=10)
b3 = tk.Button(win, text='查询', width=10, font=("等线", 16), bg='grey', bd=5, fg='white',
command=lambda :chaxun_show()).pack(fill='x', padx=10, pady=10)
win.mainloop()