简体   繁体   English

tkinter ttk 遍历树视图

[英]tkinter ttk iterating through treeview

I an using a tkinter ttk GUI to present data on files in a server.我使用 tkinter ttk GUI 来显示服务器中文件的数据。 The information is stored in a ttk treeview and presented as a table.信息存储在 ttk 树视图中并显示为表格。 The goal is for the user to be able to filter these rows so that functions can be performed only on those visible in the treeview after the user is done filtering.目标是让用户能够过滤这些行,以便在用户完成过滤后只能对树视图中可见的那些行执行功能。

Problem is, I can't find a way to iterate through the treeview.问题是,我找不到遍历树视图的方法。 I need to be able to to do something like this:我需要能够做这样的事情:

def filterTreeview(treeviewToFilter, tvColumn, stringVariable):
    for tvRow in treeviewToFilter:
        if tvRow.getValue(tvColumn) != stringVariable:
            tvRow.detach()

How can I achieve this?我怎样才能做到这一点?

As a secondary question, does anybody know of a better way to do this?作为次要问题,有人知道更好的方法吗? Is there any reason to use a treeview rather than a simple array?有什么理由使用树视图而不是简单的数组吗? What about making the filter on an array of data and then re-creating the treeview table from scratch?如何对数据数组进行过滤,然后从头开始重新创建树视图表?

I've spent a lot of time reading tutorials looking for information but I've not been successful in understanding the way to use data in a treeview so far:我花了很多时间阅读教程以寻找信息,但到目前为止我还没有成功理解在树视图中使用数据的方式:

python ttk treeview sort numbers http://www.tkdocs.com/tutorial/tree.html python ttk 树视图排序数字http://www.tkdocs.com/tutorial/tree.html

https://fossies.org/dox/Python-3.5.2/classtkinter_1_1ttk_1_1Treeview.html https://fossies.org/dox/Python-3.5.2/classtkinter_1_1ttk_1_1Treeview.html

To iterate through a treeview's individual entries, get a list of treeview item 'id's and use that to iterate in a 'for' loop:要遍历树视图的各个条目,请获取树视图项“id”的列表,并使用它在“for”循环中进行迭代:

#Column integer to match the column which was clicked in the table
col=int(treeview.identify_column(event.x).replace('#',''))-1

#Create list of 'id's
listOfEntriesInTreeView=treeview.get_children()

for each in listOfEntriesInTreeView:
    print(treeview.item(each)['values'][col])  #e.g. prints data in clicked cell
    treeview.detach(each) #e.g. detaches entry from treeview

This does what I need but if there is a better way, please let me know.这满足了我的需求,但如果有更好的方法,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM