My_dir='' # string to hold directory path My_w.geometry("400x300") # Size of the window Tkinter showing directory and file structure in Treeview of the user selected path using filedialog Note how the value of i is maintained across the directory listing and then file listing so unique iid values can be assigned to each item in Treeview. Trv.insert(i, 'end',iid='sub'+str(f2i),values ="-" + f2)Īfter showing the directories with child nodes, we can display all the files inside the user selected directory. Path2=path+'/'+d # Path for sub directoryįiles2=next(os.walk(path2)) # file list of Sub directory We used str() to convert integer value of f2i to string while assigning unique iid to child nodes. Watch how the value of variable i is used as iid of parent item and same is used as parent iid while inserting the child items.Įach child item has different value of iid ( value of f2i ). On expansion of parent node the files inside the parent ( main directory ) is shown. ![]() We can check here how to add child node to a parent row of Treeview. ![]() Trv.delete(item) Showing directories with child node Here the variable path holds the system path as string.ĭirnames=next(os.walk(path)) # list of directoriesįiles=next(os.walk(path)) # list of files Using Treeviewīy using Treeview we can display the directories and files of the user selected path.įirst we will delete all items of the Treeview before adding new files or directories. After selecting the directory we can collect the sub-directories and files inside the selected directory by using os.walk(). We can use askdirectory() to select any directory in your system. Directory and file structure showing in Treeview
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |