0

Hello I would like to do something like that, can you suggest a function that will allow me to do this? Thanks

What I have on the window:

- GrandParents
    - Parents
        Child1
        Child2
    - Parents2
        Child3
- GrandParents2
    Children

...

What I would like to get in the console:

GrandParents_Parents_Child1
GrandParents_Parents_Child2
GrandParents_Parents2_Child3
GrandParents2_Children
...

My code:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.title("Treeview")
entry = tk.Entry(root)
tv = ttk.Treeview()
menu = tk.Menu(root, tearoff = False)

def add():
    if not tv.exists(entry.get()) and entry.get() != "":
        if tv.selection() == ():
            tv.insert('', 'end', entry.get(), text=entry.get())
        else:
            tv.insert(tv.selection()[0], 'end', entry.get(), text=entry.get())

def popup(event):
    menu.tk_popup(event.x_root, event.y_root)

def unselect(event):
    if len(tv.selection()) > 0:
        tv.selection_remove(tv.selection()[0])

menu.add_command(label="Add", command=add)
tv.bind("<Button-3>", popup)
tv.bind("<Button-1>", unselect)

tv.pack()
entry.pack()
root.mainloop()
3
  • Can you show us the code snippet you used for displaying tree view? Commented Dec 21, 2020 at 14:27
  • it's ok i've added my code. ;) Commented Dec 21, 2020 at 14:38
  • Nobody have the answer ? x) Commented Dec 21, 2020 at 17:11

1 Answer 1

1

I have provided a small example how you can achive this. Use the optional parameter iid of an item to identfy it and use the parent(item) method of the Treeview to get the information:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.title("Treeview")
entry = tk.Entry(root)
tv = ttk.Treeview()
#list of all parents of the selected item
parents = []
def get_parent(item):
    parent = tv.parent(item)
    parents.append(parent) #add parent to list
    if tv.parent(parent): #if parent has a parent
        get_parent(parent)
def get_tree(event):
    selected = tv.selection()
    if tv.parent(selected):
        get_parent(selected)
    print(f'the parantes of {selected} are {parents}')
    parents.clear()#clear list after printing
##insert some items and use the optional parameter idd i.e. Grandparent
tv.insert('', 'end', 'Grandparent', text='Grandparent')
tv.insert('Grandparent', 'end', 'Parent', text='Parent')
tv.insert('Parent', 'end', 'Child', text='Child')
##bind treeview select event
tv.bind('<<TreeviewSelect>>', get_tree)
menu = tk.Menu(root, tearoff = False)

def add():
    if not tv.exists(entry.get()) and entry.get() != "":
        if tv.selection() == ():
            tv.insert('', 'end', entry.get(), text=entry.get())
        else:
            tv.insert(tv.selection()[0], 'end', entry.get(), text=entry.get())

def popup(event):
    menu.tk_popup(event.x_root, event.y_root)

def unselect(event):
    if len(tv.selection()) > 0:
        tv.selection_remove(tv.selection()[0])

menu.add_command(label="Add", command=add)
tv.bind("<Button-3>", popup)
tv.bind("<Button-1>", unselect)

tv.pack()
entry.pack()
root.mainloop()
Sign up to request clarification or add additional context in comments.

3 Comments

Mhmm That doesn't really answer my question 😅, but it's part of it.
What is missing?
It's ok I managed to do what I wanted with your code and the get_all_children of another thread that talks about Treeview on stackoverflow. Thanks again for your time. 👍

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.