Progress Bar not Working - Copy Paste application

19 Views Asked by At

I'm an amateur python writer; I've trying to write a COPY GUI with a progress bar. You may think this may be silly but the operators that will use this simple GUI are not that familiar with computer.

Anyway, for some reason, my GUI is not pop-up the Progress bar. please see below my code:

import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
from tkinter import messagebox
import shutil
import threading
from tqdm import tqdm
import os

class FileCopyGUI:
    def __init__(self, master):
        self.master = master
        self.master.title("File Copy Tool")

        self.source_label = tk.Label(master, text="Source:")
        self.source_label.grid(row=0, column=0, sticky=tk.W, padx=5, pady=5)

        self.source_entry = tk.Entry(master, width=50)
        self.source_entry.grid(row=0, column=1, columnspan=2, padx=5, pady=5)

        self.source_button = tk.Button(master, text="Browse", command=self.browse_source)
        self.source_button.grid(row=0, column=3, padx=5, pady=5)

        self.destination_label = tk.Label(master, text="Destination:")
        self.destination_label.grid(row=1, column=0, sticky=tk.W, padx=5, pady=5)

        self.destination_entry = tk.Entry(master, width=50)
        self.destination_entry.grid(row=1, column=1, columnspan=2, padx=5, pady=5)

        self.destination_button = tk.Button(master, text="Browse", command=self.browse_destination)
        self.destination_button.grid(row=1, column=3, padx=5, pady=5)

        self.copy_button = tk.Button(master, text="Copy", command=self.copy_files)
        self.copy_button.grid(row=2, column=1, columnspan=2, padx=5, pady=5)

    def browse_source(self):
        source_folder = filedialog.askdirectory()
        if source_folder:
            self.source_entry.delete(0, tk.END)
            self.source_entry.insert(0, source_folder)

    def browse_destination(self):
        destination_folder = filedialog.askdirectory()
        if destination_folder:
            self.destination_entry.delete(0, tk.END)
            self.destination_entry.insert(0, destination_folder)

    def copy_files(self):
        source = self.source_entry.get()
        destination = self.destination_entry.get()

        if not os.path.exists(source):
            messagebox.showerror("Error", "Source directory does not exist.")
            return
        if not os.path.exists(destination):
            messagebox.showerror("Error", "Destination directory does not exist.")
            return

        def copy():
            try:
                num_files = sum(len(files) for _, _, files in os.walk(source))
                with tqdm(total=num_files, unit='file') as self.master:
                    for root, _, files in os.walk(source):
                        for file in files:
                            src_file = os.path.join(root, file)
                            dst_file = os.path.join(destination, os.path.relpath(src_file, source))
                            shutil.copy2(src_file, dst_file)
                            self.master.update(1)
                self.progress_window.destroy()
                messagebox.showinfo("Success", "Copy completed")
                self.clear_gui()
            except Exception as e:
                messagebox.showerror("Error", f"An error occurred: {str(e)}")
                self.progress_window.destroy()

        thread = threading.Thread(target=copy)
        thread.start()

        self.progress_window.protocol("WM_DELETE_WINDOW", self.disable_close)  # Disable closing of progress window

    def disable_close(self):
        messagebox.showinfo("Info", "Please wait until the copy process is completed.")

    def clear_gui(self):
        self.source_entry.delete(0, tk.END)
        self.destination_entry.delete(0, tk.END)

def main():
    root = tk.Tk()
    app = FileCopyGUI(root)
    root.mainloop()

if __name__ == "__main__":
    main()

Thanks for your help and support.

0

There are 0 best solutions below