tkinter 中treeviw 如何刷新数据
本帖最后由 rsj0315 于 2020-12-30 10:39 编辑tkinter做了一个ui,
ui上有一个entry,一个button,和一个treeview
点击按钮treeview显示数据,
在entry输入内容,点击按钮,刷新treeview,显示筛选后的内容。
现在是treeview不能刷新。有谁知道怎么实现这个功能吗?
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 =
# print(ac)
# 返回数据
data = tuple(map(tuple, df.values))
# print(data)
# 返回行数
a = df.shape
# 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.astype(str).str.contains(x)]
# ##标题栏
name = tuple(df.columns.tolist())
# print(name)
ac =
# print(ac)
# 返回数据
data = tuple(map(tuple, df.values))
# print(data)
# 返回行数
a = df.shape
# 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, width=20, anchor='w')
elif i == 3:
tv.column(ac, width=20, anchor='w')
else:
tv.column(ac, width=100, anchor='c')
tv.heading(ac, text=name)
for i in range(a):
tv.insert('', 'end', values=data)
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, width=20, anchor='w')
elif i == 3:
tv.column(ac, width=20, anchor='w')
else:
tv.column(ac, width=100, anchor='c')
tv.heading(ac, text=name)
for i in range(a):
tv.insert('', 'end', values=data)
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()
@Twilight6 @zltzlt 写的可能有点繁琐了。
主要是想,点击一个按钮显示dataframe,在entry输入数值,点击查询,treeview显示查询后的数据。
@hrp 帮忙看看这个能弄么? rsj0315 发表于 2020-12-30 12:38
@hrp 帮忙看看这个能弄么?
我不会tk{:10_250:}
页:
[1]