Welcome back again, in this chapter we will create a message box to tell the user how many duplicate files have been deleted from each selected file. The game plan here is simple, we will attach a message variable to the label widget and pass that widget to each remove thread instance so the program can modify the content of that message by adding in the selected file name as well as the total number of duplicate files that have been remove from the selected folder. That modify message will then be shown by the messagebox on the main thread at the end of each file deleting process. The first file we need to edit is the main file where we will pass in the tkinter’s label object with the message variable to each new remove thread instance which the program has created, the remove thread instance will then modify the content of that same message which includes the total number of duplicate files that have been deleted together with the file name of the original selected file. We need not worry too much about whether those threads will compete with each other to modify the content of the message or not because one process will finish before another so therefore there is always a time gap in between the process in this case. Here is the new version of the main file.
from tkinter import * from tkinter import filedialog from Remove import Remove import os from tkinter import messagebox win = Tk() # 1 Create instance win.title("Multitas") # 2 Add a title win.resizable(0, 0) # 3 Disable resizing the GUI win.configure(background='black') # 4 change background color # 5 Create a label aLabel = Label(win, text="Remove duplicate", anchor="center") aLabel.grid(column=0, row=1) aLabel.configure(foreground="white") aLabel.configure(background="black") aLabel.message = '' # 6 Create a selectFile function to be used by button def selectFile(): fullfilenames = filedialog.askopenfilenames(initialdir="/", title="Select a file") # select multiple files from the hard drive if(fullfilenames != ''): fullfilenamelist = win.tk.splitlist(fullfilenames) directory_path = os.path.dirname(os.path.abspath(fullfilenamelist[0])) os.chdir(directory_path) # change the directory to the selected file directory folder = filedialog.askdirectory() # 7 open a folder then create and start a new remove thread to delete the duplicate file folder = folder.replace('/', '\\') # 8 this is for the windows separator only if(folder != '' and folder != os.getcwd()): for fullfilename in fullfilenamelist: if(fullfilename != ''): filename = fullfilename.split('/')[-1] remove = Remove(folder, filename, fullfilename, directory_path, aLabel) remove.start() remove.join() messagebox.showinfo('Remove duplicate files', aLabel.message) else: messagebox.showinfo("Error", "Kindly select one folder and it must be a different one") return else: messagebox.showinfo("Select file", "You need to select a file!") return # 9 Adding a Button action = Button(win, text="Select File", command=selectFile) action.grid(column=0, row=0) action.configure(background='brown') action.configure(foreground='white') win.mainloop() # 10 start GUI
And here is the edit version of the remove thread class.
import threading import os import filecmp from tkinter import messagebox class Remove(threading.Thread): def __init__(self, massage, filename, fullfilename, directory_path, aLabel): threading.Thread.__init__(self) self.massage = massage self.filename, self.file_extension = os.path.splitext(filename) self.fullfilename = fullfilename self.directory_path = directory_path self.count = 0 self.aLabel = aLabel def run(self): filepaths = os.listdir(self.massage) for filepath in list(filepaths): os.chdir(self.massage) if(os.getcwd() != self.directory_path): # make sure that we will not delete the same file in the selected file directory if(os.path.isfile(filepath)): filename, file_extension = os.path.splitext(filepath) self.remove_file(file_extension, filepath) else: self.delete_duplicate(os.path.join(self.massage, filepath)) else: continue self.aLabel.message = 'Removed ' + str(self.count) + ' duplicate : ' + self.filename # show this message box each time a set of duplicate files have been remove def delete_duplicate(self, folder): # sub method to pass folder to filepaths = os.listdir(folder) for filepath in list(filepaths): os.chdir(folder) if(os.getcwd() != self.directory_path): if(os.path.isfile(filepath)): filename, file_extension = os.path.splitext(filepath) self.remove_file(file_extension, filepath) else: self.delete_duplicate(os.path.join(folder, filepath)) else: continue def remove_file(self, file_extension, filepath): if (file_extension == self.file_extension): if filecmp.cmp(filepath, self.fullfilename, shallow=False): os.remove(filepath) self.count += 1
Now each remove thread will manage it’s own business so there is no conflict with the count variable and the old message text will be replaced by the new one by the next process which changes it. That is basically it for this remove duplicate project, we just need to do some decorations on the user interface in the next chapter then we are ready to pack it up and use it on our windows’ desktop.
from Planet Python
via read more
No comments:
Post a Comment