鱼C论坛

 找回密码
 立即注册
查看: 3499|回复: 3

tkinter 中treeviw 如何刷新数据

[复制链接]
发表于 2020-12-29 17:13:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 rsj0315 于 2020-12-30 10:39 编辑

tkinter做了一个ui,
ui上有一个entry,一个button,和一个treeview
点击按钮treeview显示数据,
在entry输入内容,点击按钮,刷新treeview,显示筛选后的内容。

现在是treeview不能刷新。有谁知道怎么实现这个功能吗?

  1. import pandas as pd
  2. import tkinter.ttk as ttk
  3. import tkinter as tk


  4. def readcvs():
  5.     df= pd.read_csv(r'C:\Users\renro001\Desktop\DEMO.txt',sep='\t')
  6.     print(df)
  7.     # ##标题栏
  8.     name = tuple(df.columns.tolist())
  9.     # print(name)
  10.     ac = [str(i) for i in range(len(name))]
  11.     # print(ac)
  12.     # 返回数据
  13.     data = tuple(map(tuple, df.values))
  14.     # print(data)
  15.     # 返回行数
  16.     a = df.shape[0]
  17.     # print(a)
  18.     return name, data, a, ac
  19. def readcvs_chaxun(x=''):
  20.     df= pd.read_csv(r'C:\Users\renro001\Desktop\DEMO.txt',sep='\t')
  21.     print(df)
  22.     if x:
  23.         df = df[df['A'].astype(str).str.contains(x)]
  24.     # ##标题栏
  25.     name = tuple(df.columns.tolist())
  26.     # print(name)
  27.     ac = [str(i) for i in range(len(name))]
  28.     # print(ac)
  29.     # 返回数据
  30.     data = tuple(map(tuple, df.values))
  31.     # print(data)
  32.     # 返回行数
  33.     a = df.shape[0]
  34.     # print(a)
  35.     return name, data, a, ac

  36. win = tk.Tk()
  37. win.title('xxx')
  38. win.geometry("1200x600+400+20")

  39. def all_show():
  40.     name,data,a,ac = readcvs()
  41.     tv = ttk.Treeview(win, columns=name, show='headings', height=150)
  42.     ybar = tk.Scrollbar(win, orient=tk.VERTICAL,
  43.                         command=tv.yview)
  44.     xbar = tk.Scrollbar(win, orient=tk.HORIZONTAL,
  45.                         command=tv.xview)
  46.     tv.configure(yscroll=ybar.set)
  47.     tv.configure(xscroll=xbar.set)
  48.     ybar.pack(side=tk.RIGHT, fill=tk.Y)
  49.     xbar.pack(side=tk.BOTTOM, fill=tk.X)
  50.     tv.pack(fill='x', padx=10, pady=10)
  51.     for i in range(len(name)):
  52.         if i == 1:
  53.             tv.column(ac[i], width=20, anchor='w')
  54.         elif i == 3:
  55.             tv.column(ac[i], width=20, anchor='w')

  56.         else:
  57.             tv.column(ac[i], width=100, anchor='c')
  58.         tv.heading(ac[i], text=name[i])
  59.     for i in range(a):
  60.         tv.insert('', 'end', values=data[i])
  61. cv1 = tk.StringVar()
  62. x=cv1.get()
  63. def chaxun_show():
  64.     global x
  65.     name,data,a,ac = readcvs_chaxun(x=cv1.get())
  66.     tv = ttk.Treeview(win, columns=name, show='headings', height=150)
  67.     ybar = tk.Scrollbar(win, orient=tk.VERTICAL,
  68.                         command=tv.yview)
  69.     xbar = tk.Scrollbar(win, orient=tk.HORIZONTAL,
  70.                         command=tv.xview)
  71.     tv.configure(yscroll=ybar.set)
  72.     tv.configure(xscroll=xbar.set)
  73.     ybar.pack(side=tk.RIGHT, fill=tk.Y)
  74.     xbar.pack(side=tk.BOTTOM, fill=tk.X)
  75.     tv.pack(fill='x', padx=10, pady=10)
  76.     for i in range(len(name)):
  77.         if i == 1:
  78.             tv.column(ac[i], width=20, anchor='w')
  79.         elif i == 3:
  80.             tv.column(ac[i], width=20, anchor='w')

  81.         else:
  82.             tv.column(ac[i], width=100, anchor='c')
  83.         tv.heading(ac[i], text=name[i])
  84.     for i in range(a):
  85.         tv.insert('', 'end', values=data[i])

  86. b2 = tk.Button(win, text='显示所有', width=10, font=("等线", 16), bg='grey', bd=5, fg='white',
  87.                    command=lambda :all_show()).pack(fill='x', padx=10, pady=10)
  88. label3 = tk.Label(win, text='按A列数据筛选:', font=("等线", 16)).pack(fill='x', padx=10, pady=10)
  89. entry1 = tk.Entry(win, font=("等线", 16),textvariable=cv1).pack(fill='x', padx=10, pady=10)
  90. b3 = tk.Button(win, text='查询', width=10, font=("等线", 16), bg='grey', bd=5, fg='white',
  91.                    command=lambda :chaxun_show()).pack(fill='x', padx=10, pady=10)
  92. win.mainloop()
复制代码


DEMO.txt

53 Bytes, 下载次数: 3

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-12-30 10:41:42 | 显示全部楼层
@Twilight6 @zltzlt 写的可能有点繁琐了。
主要是想,点击一个按钮显示dataframe,在entry输入数值,点击查询,treeview显示查询后的数据。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-12-30 12:38:06 | 显示全部楼层
@hrp 帮忙看看这个能弄么?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-30 12:56:26 From FishC Mobile | 显示全部楼层
rsj0315 发表于 2020-12-30 12:38
@hrp 帮忙看看这个能弄么?

我不会tk
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-29 13:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表