Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there any way to get the full path of a file selected in python Tkinter Treeview

I have seen this thread Python tkinter treeview get full path, But after trying this I got only the path from previous parent node. What if directory tree has lots of branches. I need the full path of a selection.

For eg.

Sample Treeview directory tree

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here when I click the beaglebone I got path D:\eclipse\beaglebone. But when .metadata is clicked I got path beaglebone\.metadata. But my expectation is to get D:\eclipse\beaglebone\.metadata.

OnDoubleClick() I need to get the full path. Do anyone has any idea?

My code is,

class DirectoryTree(object):
    def __init__(self, frame, path):
        self.nodes = dict()
        self.tree = ttk.Treeview(frame,  height="18")
        self.tree.grid()

        self.tree.heading('#0', text='Select file', anchor='nw')
        self.tree.column('#0', width=225, minwidth=400)

        # Scrollbar
        ysb = ttk.Scrollbar(frame, orient='vertical', command=self.tree.yview)
        ysb.grid(row = 0, column = 1, sticky='ns')
        xsb = ttk.Scrollbar(frame, orient='horizontal', command=self.tree.xview)
        xsb.grid(row = 1, column = 0, sticky='we')
        self.tree.configure(xscroll=xsb.set, yscroll=ysb.set)

        abspath = os.path.abspath(path)
        self.insert_node('', abspath, abspath)
        self.tree.bind('<<TreeviewOpen>>', self.open_node)
        self.tree.bind("<Double-1>", self.OnDoubleClick)

    def insert_node(self, parent, text, abspath):
        node = self.tree.insert(parent, 'end', text=text, open=False)
        if os.path.isdir(abspath):
            self.nodes[node] = abspath
            self.tree.insert(node, 'end')

    def open_node(self, event):
        node = self.tree.focus()
        abspath = self.nodes.pop(node, None)
        if abspath:
            self.tree.delete(self.tree.get_children(node))
            for p in os.listdir(abspath):
                self.insert_node(node, p, os.path.join(abspath, p))

    def delete_nodes(self):
        x = self.tree.get_children()
        for item in x:
            self.tree.delete(item)

    def OnDoubleClick(self, event):
        item = self.tree.selection()[0]
        parent_iid = self.tree.parent(item)
        node = self.tree.item(parent_iid)['text']
        i = self.tree.item(item,"text")
        path=os.path.join(node, i)
        
        if (os.path.isfile(path)):
            print("file:", path)
            submit_file(i)
        else:
            print("dir:", path)    

>Solution :

You need to go backward the tree until reaching root:

    def OnDoubleClick(self, event):
        item = self.tree.selection()[0]
        parent_iid = self.tree.parent(item)
        node = []
        # go backward until reaching root
        while parent_iid != '':
            node.insert(0, self.tree.item(parent_iid)['text'])
            parent_iid = self.tree.parent(parent_iid)
        i = self.tree.item(item, "text")
        path = os.path.join(*node, i)
        ...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading