Welcome back to the multitas project which we have already created two features for this application in the previous articles 1) Remove duplicate files 2) Move file from one folder to another. In this chapter, we are just going to tidy up the buttons on the user interface before we continue to build the next task in the next chapter. What we are going to do here is to create a button container to keep all the buttons that we will use in our program. Below is the full source code which will result in this below outcome.
from tkinter import *
from tkinter import filedialog
from Remove import Remove
import os
from tkinter import messagebox
import webbrowser
from Copy import Copy
win = Tk() # 1 Create instance
win.title("Multitas") # 2 Add a title
#win.iconbitmap(os.path.abspath('icon.ico'))
win.resizable(0, 0) # 3 Disable resizing the GUI
win.configure(background='black') # 4 change background color
#win.iconbitmap(('icon.ico'))
# 5 Create a label
aLabel = Label(win, text="Select a task", anchor="center", padx=13, pady=10, relief=RAISED,)
aLabel.grid(column=0, row=0, sticky=W+E)
aLabel.configure(foreground="white")
aLabel.configure(background="black")
aLabel.configure(wraplength=90)
aLabel.message = ''
# 6 Create a selectFile function to be used by action 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 the 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
# 6 Create a copyFile function to move a file from one folder to another
def copyFile():
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 Copy thread to move the file from one directory to another one
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]
copy = Copy(folder, filename, fullfilename)
copy.start()
copy.join()
messagebox.showinfo('Move the file ', 'File has been moved to new destination')
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
def openLink(): # start a new link
webbrowser.open_new("http://codingdirectional.info/2018/12/12/remove-duplicate-files-project-is-ready/")
# 9 Adding a Button
button_frame = Frame(win, bg='black')
button_frame.grid(column=0, row=1, sticky=E+W)
action = Button(button_frame, text="Remove", command=selectFile, padx=2)
action.grid(column=0, row=0, sticky=E+W)
action.configure(background='black')
action.configure(foreground='white')
action_move = Button(button_frame, text="Move", command=copyFile, padx=2)
action_move.grid(column=1, row=0, sticky=E+W)
action_move.configure(background='black')
action_move.configure(foreground='white')
action_link = Button(button_frame, text="Manual", command=openLink, padx=2)
action_link.grid(column=2, row=0, sticky=E+W)
action_link.configure(background='black')
action_link.configure(foreground='white')
win.mainloop() # 10 start GUI
That is basically it, now the buttons will be more organize than before and it is time for us to move on to the next chapter.
from Planet Python
via read more
No comments:
Post a Comment