I've just started learning Python and I'm trying to achieve this in tkinter
:
- Get user to select any multiple number of files in any directory locations and store it in another folder
I'm not sure if there's any other more efficient ways of doing this but I've tried solving this by:
- Getting the user to input how many files they are searching for (I set 2 as default)
- loop over the number of files and ask for the user to select the files
- send all the files to the new location
The problem is I can't quite get the storing the files and looping to work well. Here's the code:
import tkinter as tk
from tkinter import filedialog, ttk
import shutil
class Getfiles():
def __init__(self):
# initialising the screen name, size title and icon
root = tk.Tk()
root.geometry('450x100')
root.resizable(0, 0)
root.configure(bg='#002060')
# initialising integer value to pass to select_files method
self.var = tk.IntVar()
self.var.set('2')
# Initialising the frame to insert our widget
frame_three = tk.Frame(root, bg='#002060')
frame_three.pack(side='top', fill='both')
# setting label to tell user to input no. of files
num_label = ttk.Label(frame_three, text='No. of files: ', background='#002060', foreground='white')
num_label.pack(side='left', padx=(40, 10), pady=(20, 20))
# setting number of files user wants to fetch
files_num = ttk.Entry(frame_three, width=3, textvariable=self.var)
files_num.pack(side='left', padx=(10, 40), pady=20)
# get user to select the files listed
select_button = ttk.Button(frame_three, text='Select files', width=30, command=self.select_files)
select_button.pack(side='left', padx=(50, 10))
root.mainloop()
def select_files(self):
file_list = []
for i in range(self.var.get()):
file_chosen = filedialog.askopenfilenames(title='Choose a file')
file_list = list(file_chosen)
list += file_list
self.copy(file_list)
def copy(self, file_list):
destination = filedialog.askdirectory()
for file in file_list:
shutil.copy(file, destination)
if __name__ == '__main__':
Getfiles()