【问题标题】:Display Treeview data on click and hide on click - Tkinter单击时显示 Treeview 数据并在单击时隐藏 - Tkinter
【发布时间】:2021-01-01 16:56:38
【问题描述】:

我有这个小 Tkinter 树视图程序,其中数据以表格的形式显示,那里还有一个按钮。所以我想知道的是能够通过单击相同的显示按钮来隐藏和显示表格的数据。就像我点击一次它显示,当我再次点击它时,它会隐藏它的数据。

对于数据,我的意思是它显示的所有 ID 和名称

from tkinter import ttk
import tkinter as tk
from tkinter import *

ID = [1,2,3,4,5, 6, 7, 8, 9]
Names = ['Tom', 'Rob', 'Tim', 'Jim', 'Kim', 'Kim', 'Kim', 'Kim']

window = tk.Tk()
window.state('zoomed')
treev = ttk.Treeview(window, selectmode ='browse')
treev.place(width= 250, height= 500, x=300, y=100)


verscrlbar = ttk.Scrollbar(window,
                           orient ="vertical",
                           command = treev.yview)

verscrlbar.pack(side ='right', fill ='y')
treev.configure(yscrollcommand = verscrlbar.set)


treev["columns"] = ('1', '2')

treev['show'] = 'headings'

treev.column("1", width = 90, anchor ='c')
treev.column("2", width = 90, anchor ='c')


treev.heading("1", text ="ID")
treev.heading("2", text ="Names")

for x, y in zip(ID, Names):
    treev.insert("", 'end', values =(x, y))

displaybutton = Button(window, text="Display", width=15, height=2, background= 'brown')
displaybutton.place(x=600, y=400)

window.mainloop()

【问题讨论】:

  • 应该隐藏所有行还是选择的行?
  • 所有行,即所有名称和 ID
  • 还应该显示列名吗?
  • 是应该只隐藏行

标签: python python-3.x tkinter treeview


【解决方案1】:

其中一种方法是逐行分离。为此,您需要将所有项目 iid 存储在一个数组中。 treev.get_children() 将返回所有项目 ID。您稍后可以使用 detach()reattach()(或 move())删除和显示行

from tkinter import ttk
import tkinter as tk
from tkinter import *

def hide():
    
    global a
        
    if not treev.get_children():
       for  x, child in enumerate(a):
           treev.reattach(item=child, parent=treev.parent(a[0]), index=x) # or you could give parent as an empty string like parent=''

    else:
        for child in a:
            treev.detach(child)
 

window = tk.Tk()
window.state('zoomed')
treev = ttk.Treeview(window, selectmode ='browse')
treev.place(width= 250, height= 500, x=300, y=100)


verscrlbar = ttk.Scrollbar(window,
                           orient ="vertical",
                           command = treev.yview)

verscrlbar.pack(side ='right', fill ='y')
treev.configure(yscrollcommand = verscrlbar.set)

clicked = False
treev["columns"] = ('1', '2')

treev['show'] = 'headings'

ID = [1,2,3,4,5, 6, 7, 8, 9]
Names = ['Tom', 'Rob', 'Tim', 'Jim', 'Kim', 'Kim', 'Kim', 'Kim']

treev.column("1", width = 90, anchor ='c')
treev.column("2", width = 90, anchor ='c')


treev.heading("1", text ="ID")
treev.heading("2", text ="Names")

for x, y in zip(ID, Names):
    treev.insert("", 'end', values =(x, y))

a = treev.get_children()

displaybutton = Button(window, text="Display", width=15, height=2, background= 'brown', command=hide)
displaybutton.place(x=600, y=400)

window.mainloop()

【讨论】:

  • 谢谢 我明白了,请问有没有其他方法?
  • @YashuCッ treev['display']='' 但这将使包括列名在内的所有行都消失。把它带回来treev[display]='#all'
  • 您好,如果您知道删除插入的值并再次插入值可以做到这一点,我想知道吗?
  • @YashuCッ 是的,你可以,但这不是必需的,因为你只想隐藏它们。
  • 实际上我正在做一个项目,我必须以类似的方式显示学生数据,但是在尝试这样做时我遇到了错误,而且项目很大。
【解决方案2】:

我们只需在单击时将值插入树视图中即可创建此功能,下次单击时,我们可以删除在树视图中插入的所有内容。

a = 2
def showhide():
    global a
    if a%2 == 0:
        for x, y in zip(ID, Names):
            treev.insert("", 'end', values =(x, y))

        a= a+1
    else:
        c = treev.get_children()
        for child in c:
            treev.delete(child)
        a= a+1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-18
    相关资源
    最近更新 更多