2

I am making a script which will copy directory or files to other directory.I used list directory os.listdir() to list all the files of directory and made all files checkbutton but i want to use Tree Structure instead of os.listdir().I did some research about it on google but did't get anything. Anyone can tell me how to do it ?

Code:

import tkinter as tk
import os
import tkinter.filedialog
import shutil

i = 0

class ScrolledFrame(tk.Frame):

    def __init__(self, parent, vertical=True, horizontal=False):
        global ent1
        global ent2
        super().__init__(parent)

        self._canvas = tk.Canvas(self)
        self._canvas.grid(row=0, column=0)

        self._vertical_bar = tk.Scrollbar(self, orient="vertical", 
command=self._canvas.yview)
        if vertical:
            self._vertical_bar.grid(row=0, column=1, sticky="ns")
        self._canvas.configure(yscrollcommand=self._vertical_bar.set)

        self._horizontal_bar = tk.Scrollbar(self, orient="horizontal", 
command=self._canvas.xview)
        if horizontal:
            self._horizontal_bar.grid(row=1, column=0, sticky="we")
        self._canvas.configure(xscrollcommand=self._horizontal_bar.set)

        self.frame = tk.Frame(self._canvas)
        self._canvas.create_window((0, 0), window=self.frame, anchor="nw")

        self.frame.bind("<Configure>", self.resize)

        btn1 = tkinter.Button(root, text="Source Path", command = call)
        btn1.pack(fill="both", expand=1)

        ent1 = tkinter.Entry(root)
        ent1.pack(fill="both", expand=1)

        btn1 = tkinter.Button(root, text="destination",command = des)
        btn1.pack(fill="both", expand=1)

        ent2 = tkinter.Entry(root)
        ent2.pack(fill="both", expand=1)

        cpy = tkinter.Button(root, text="Copy",command = copy_file)
        cpy.pack(fill="x")


        self.pack()

    def resize(self, event=None): 
        self._canvas.configure(scrollregion=self._canvas.bbox("all"))


#This is not in class    
def call():
     global filez
     global buttons
     global i


     buttons = []
     filez = tkinter.filedialog.askdirectory(parent=root,initialdir = 
"/",title='Choose a file')
     ent1.delete(0,"end") 
     ent1.insert(0, filez)

     dirs = os.listdir(filez)


     # remove previous IntVars
     intvar_dict.clear()

     # remove previous Checkboxes      
     for cb in checkbutton_list:
        cb.destroy()
     checkbutton_list.clear()



     for filename in dirs:

         # create IntVar for filename and keep in dictionary
         intvar_dict[filename] = tk.IntVar()

         # create Checkbutton for filename and keep on list
         c = tk.Checkbutton(sf.frame, text=filename, 
variable=intvar_dict[filename])
         c.pack()
         checkbutton_list.append(c)
         buttons.append((intvar_dict[filename],filename))
     #check all
     if  i == 0:    
         var = tk.IntVar()
         c1 = tk.Checkbutton(root, text="select all", 
variable=var,command=select_all,activebackground='red',foreground='red')
         c1.pack(side="left")
         i = i + 1

def des():
    global destination

    destination = tkinter.filedialog.askdirectory(parent=root, title='Choose a Path')
    ent2.delete(0,"end")
    ent2.insert(20, destination)

def copy_file():
    for key, value in intvar_dict.items():
        if value.get() > 0:
            src_path = filez +"//"+key
            if (os.path.isfile(src_path)) == True: 
                shutil.copy(src_path,destination) 
            elif (os.path.isdir(src_path)) == True:

                 shutil.copytree(src_path,destination+"//"+key)
            else:
                pass
    for item in buttons:
        v , n = item
        if v.get():
            v.set(0)


def select_all(): # Corrected
    for item in buttons:
        v , n = item
        if v.get():
            v.set(0)
        else:
            v.set(1)            



root = tk.Tk()

intvar_dict = {}
checkbutton_list = []

sf = ScrolledFrame(root)

#call()

root.mainloop()

Output:

enter image description here

Expected Ouput:

I want tree structure in place of List of files as a checkbutton

Note:Class call() is giving me checkbutton of files

furas
  • 134,197
  • 12
  • 106
  • 148
python_fan
  • 113
  • 2
  • 15
  • 2
    with [os.walk()](https://docs.python.org/3/library/os.html#os.walk) you can get all files in folder and subfolders. – furas Nov 22 '17 at 07:19
  • 1
    look for [tkinter.ttk.Treeview](https://docs.python.org/3/library/tkinter.ttk.html#tkinter.ttk.Treeview) and see [image](https://i.stack.imgur.com/PlrT1.jpg) – furas Nov 22 '17 at 07:22
  • I got one example of tree structure here https://stackoverflow.com/questions/16746387/tkinter-treeview-widget – python_fan Nov 22 '17 at 07:26
  • BTW: to copy all files in directory and subdirectory you can use [shutil.copytree](https://docs.python.org/3/library/shutil.html#shutil.copytree) – furas Nov 22 '17 at 07:27
  • now i am trying to add tree structure in my code – python_fan Nov 22 '17 at 07:27
  • If you want a Treeview with checkboxes, there are some examples in [this question](https://stackoverflow.com/questions/5104330/how-to-create-a-tree-view-with-checkboxes-in-python). – j_4321 Nov 22 '17 at 08:39

0 Answers0