-1

I am making a GUI which will copy folder or file from source to destination.I need to make Tree structure of Directory with checkbox to browse file.I refered this How to create a tree view with checkboxes in Python but i did't find logic to create whole folder . Please help me to solve this issue.

from tkinter import tix

import os
i = 0

class View(object):

 def __init__(self, root,path):
    self.root = root
    self.path = path
    self.makeCheckList(self.path)


 def makeCheckList(self,path1):
    global i
    self.cl = tix.CheckList(self.root, browsecmd=self.selectItem)
    self.cl.pack(fill='both',expand="yes")

    self.cl.hlist.add("CL1", text=path1)
    self.cl.setstatus("CL1", "off")
    self.check(path1)

    self.cl.autosetmode()

 def selectItem(self, item):
    print (item, self.cl.getstatus(item))

 def check(self,path1):
    global i
    self.path1 = path1
    file = os.listdir(path1)
    for p in file:
        #print(p)
        full_path = path1 +"\\"+ p
        val = "CL1.Item1"
        if os.path.isdir(full_path) != True :
            self.cl.hlist.add("CL1.Item"+ str(i), text=p)
            self.cl.setstatus("CL1.Item"+str(i), "off")
            i = i + 1
    self.dir(path1)         

 def dir(self,path1):

    global i
    self.path1 = path1
    file = os.listdir(path1)
    for folder in file:
        full_path = path1 +"\\"+ folder
        if os.path.isdir(full_path) == True :    
            self.cl.hlist.add("CL1.Item"+str(i), text=folder)
            self.cl.setstatus("CL1.Item"+str(i), "off")
            i = i + 1
            #self.dir(full_path)


def main():
   root = tix.Tk()
   root.geometry("800x400")
   view = View(root,"C:\\")
   root.update()
   root.mainloop()

if __name__ == '__main__':
   main()

My logic is not correct here.I want a recursive function which will create whole directory

python
  • 83
  • 1
  • 16

1 Answers1

2

If you want to make a tree of the content of a directory, you indeed need a recursive function. For each element of the directory, first put it in the tree and then, if it is a directory, reapply the function:

def check(self, parent, path):
    content = os.listdir(path)
    for i, item in enumerate(content):
        full_path = os.path.join(path, item)  # item absolute path
        entry = "{}.Item{}".format(parent, i)  # item path in the tree
        self.cl.hlist.add(entry, text=item)   # add item in the tree
        self.cl.setstatus(entry, "off")
        if os.path.isdir(full_path):  
            # reapply the function if the item is a directory
            self.check(entry, full_path) 

In this function, I need to keep track both of the parent directory absolute path and its path in the tree.

Then, to generate the tree, use the makeCheckList method:

def makeCheckList(self):  # no need of any extra argument here
    self.cl = tix.CheckList(self.root, browsecmd=self.selectItem)
    self.cl.pack(fill='both',expand="yes")
    # add the root directory in the tree
    self.cl.hlist.add("CL1", text=self.path)
    self.cl.setstatus("CL1", "off")
    # build the tree
    self.check("CL1", self.path)  

    self.cl.autosetmode()    

If you use python 3.5 or higher, you can replace os.listdir by os.scandir to speed up your program:

def check(self, parent, path):
    with os.scandir(path) as content:
        for i, item in enumerate(content):
            entry = "{}.Item{}".format(parent, i)
            self.cl.hlist.add(entry, text=item.name)
            self.cl.setstatus(entry, "off")
            if item.is_dir():
                self.check(entry, item.path)
j_4321
  • 15,431
  • 3
  • 34
  • 61
  • Thank you very much.Can you give me any link where i can find all commands of tix like when i click on any item it should give me its value(path) and if i check on any parent directory it should check all its child path – python Dec 07 '17 at 05:42
  • @python I don't use `tix` and I don't know any link to its documentation. However I have implemented a `CheckboxTreeview` from a `ttk.Treeview` (it 's the second answer to https://stackoverflow.com/questions/5104330/how-to-create-a-tree-view-with-checkboxes-in-python) that already propagates the checking to the children. – j_4321 Dec 07 '17 at 09:01
  • I got the code to check all but if i want to print name of checkbox then how to do it ? – python Dec 07 '17 at 09:10
  • What do you mean by the name? the whole path? – j_4321 Dec 07 '17 at 09:23
  • yes whole path because i want to copy selected folder – python Dec 07 '17 at 09:25
  • hey i got the solution.I made a dictionary and stored full path in that dictionary then when i check any box its gives me full path. Thanks for response – python Dec 07 '17 at 09:37