Welcome back to the part 2 of the python application project, if you miss the first part of the project then you can read it here. In this chapter we are going to print out the file name which we have selected earlier with the help of the custom made thread instance instead of print out that filename directly inside the selectFile method just like in the previous chapter.
First lets create a custom made thread class from the main Thread class then process the selected filename under it’s run method.
import threading class Remove(threading.Thread): def __init__(self, massage): threading.Thread.__init__(self) self.massage = massage def run(self): print(self.massage) print('program terminate!') return
Now in the main file the program will create a new instance of the Remove thread class whenever the user has clicked on the button and selected a file from the folder.
Open a folder and select a fileThis is the modify version of the main file.
from tkinter import * from tkinter import filedialog from Remove import Remove win = Tk() # 1 Create instance win.title("Multitas") # 2 Add a title win.resizable(0, 0) # 3 Disable resizing the GUI # 4 Create a label aLabel = Label(win, text="Remove duplicate file") aLabel.grid(column=0, row=0) # 5 Position the label # 6 Create a selectFile function to be used by button def selectFile(): filename = filedialog.askopenfilename(initialdir="/", title="Select file") remove = Remove(filename) # 7 create and start new thread to print the filename from the selected file remove.start() # 8 Adding a Button action = Button(win, text="Search File", command=selectFile) action.grid(column=0, row=1) # 9 Position the button win.mainloop() # 10 start GUI
The reason we need to create a thread when we search and remove the duplicate files later on in our project is because the search program will freeze up whenever we search for a very large folder which contains lots of files inside it, therefore creating a separate stand alone thread to process those files is a must.
Alright, with that we are now ready to move to the next step of our project, which is to create a search engine which will help us to remove the duplicate files on our folder.
from Planet Python
via read more
No comments:
Post a Comment