2

I am trying to later iterate through the data inside of the Treeview. I will then hope to be able to sort through it.

from tkinter import *
from tkinter.ttk import *
import pickle

root = Tk()

def treeData(event):
    children = tree.get_children()
    print(children)

entry = StringVar()
a = Entry(root, textvariable=entry)
a.grid(column=0,row=0)
a.bind("<Key>", function)

file_data = []
file = open('data.dat', 'rb')
while True:
    try:
        file_data.append(pickle.load(file))
    except EOFError:
        break
file.close()

column_names = ("Column 1", "Column 2")
tree = Treeview(root, columns=column_names)
tree['show'] = 'headings'
for x in file_data:
    a = tree.insert('', 'end', values=x)
for col in column_names: 
    tree.heading(col, text=col)

tree.grid(column=0, row=1)

In the function, called 'treeData' when I print(children) it outputs a list that looks similar to this - ('I001', 'I002', 'I003', 'I004')

I am hoping someone will know how to convert these pieces of data into what is actually shown in the row of the Treeview?

Thanks,

1
  • 2
    Without example or dummy version of loans.dat cant help much. Commented Feb 24, 2015 at 1:09

1 Answer 1

18

What you are asking is documented in the official tkinter documentation for the Treeview widget.

The get_children method returns a list of item IDs, one for each child. The item method of the treeview will return a dictionary of data for a given item. Thus, you can iterate over the values with something like this:

for child in tree.get_children():
    print(tree.item(child)["values"])
Sign up to request clarification or add additional context in comments.

2 Comments

What's interesting here is as far as I understand those I001, I002 etc. are default iid values Tkinter provides if not supplied with anything. Now, the interesting part begins when someone (that would be me) gets a brilliant idea to use list indexes when populating a tree (seems like a nice way of circumventing hassle of translating selected items back into objects they originated from) and then learns that item 0 produces back I001 instead. Probably Tkinter treats supplying 0 for iid as not supplying anything. That's my guess at least.
Also, I think if you used index values (a fine idea... at first!) will result in obscene complications later. If items are added or removed, or the list is sorted, the Iid=index values will all be wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.