Search code examples
checkboxdirectorywxpython

Put checkboxes with files and folders in a directory using wxPython GUI toolkit


I want to put checkboxes with the files and folders present in a directory. For example using GenericDirCtrl class we can obtain a directory in our GUI and can expand the directory using ExpandPath method. Now I want to put checkboxes with files and folders present in the directory to select each file/folder separately, so I can take the selected files and send the list as an argument to a proc. It should be intelligent i.e checkboxes should appear with files/folders when the directory tree is expanded and should disappear when the directory tree is collapsed to the root level(a local drive maybe).


Solution

  • I recently encountered a similar issue. The main hurdle was that when using "os.walk(pathOfDir)", it results in arrays of files/directories on basis of "level". Example, it would list all the files and directories on first level, then step into each directory and give the list of files and directories inside it, and so on. This process shall continue recursively until no more directories are left. I tackled the issue by first making a list of full path of each file returned by os.walk(pathOfDir). For that purpose, os.path.join(rootOfFile, nameOfFile) was used. Later, on the list of those files, I implemented some sort of an algorithm to use that list of files and create a HyperTreeList from it.

    You can use my following code as a sample and ask me in case of any confusion:

    import os
    import wx
    import wx.lib.agw.hypertreelist as HTL
    
    class MyFrame(wx.Frame):
    
    def __init__(self, parent):
    
        wx.Frame.__init__(self, parent, -1, pos = (0,0), size=(700, 900), title= "HyperTreeList Demo")
    
    
        # ------------------------------------------
        # Algo for creating Files' List Starts here
        # ------------------------------------------
        allFiles = []       
        for root, dirs, files in os.walk("D:\\DIRECTORY", topdown = True):  
            for name in files:  
                location = os.path.join(root, name)                 
                allFiles.append(location)       
    
        treeList= HTL.HyperTreeList(self, agwStyle= wx.TR_DEFAULT_STYLE| 0x4000 )
        treeList.AddColumn("List View")
        treeList.SetColumnWidth(0, 600)
    
        TLRoot = treeList.AddRoot ("D:", ct_type= 1)
        allDirs = []
        allDirsItem = []
        allDirs.append ("D:")
        allDirsItem.append(TLRoot)
    
    
        # --------------------------------------
        # Algo for filling Tree List Starts here
        # --------------------------------------
        for eachName in allFiles:
            nameSplit = eachName.split(os.sep)
            matchingDirFound = 0
    
            lenNS= len(nameSplit)    
            i=lenNS -1
            for eachNameSplit in reversed(nameSplit):       
                for eachDoneDir in reversed(allDirs):
                    if eachNameSplit == eachDoneDir:
                        matchingDirFound = 1
                        break     
    
                if matchingDirFound == 1: 
                    break   
                i= i-1              
    
            if matchingDirFound ==1:
                for k in range(i, lenNS-1):
                    allDirsItem.append([])
                    allDirsItem[k+1] = treeList.AppendItem (allDirsItem[k], nameSplit[k+1], ct_type= 1)
    
                    if len(allDirs)> k+1:
                        allDirs[k+1] = nameSplit[k+1]
                    else:
                        allDirs.append (nameSplit[k+1])