user3535074
user3535074

Reputation: 1338

tkinter ttk iterating through treeview

I an using a tkinter ttk GUI to present data on files in a server. The information is stored in a ttk treeview and presented as a table. 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

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

Upvotes: 2

Views: 7266

Answers (1)

user3535074
user3535074

Reputation: 1338

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:

#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.

Upvotes: 1

Related Questions