I have a project where I'm creating a simple GUI where the user chooses a source and destination directory using filedialog.askdirectory()
that does two things.
Displays the chosen path into an entry box for 'visible' verification.
Initializes two variables with each path as a string, respectively. Then these two variables are used in a script that moves any files created or modified in the source directory, within the last 24 hours, to the destination directory.
I am able to create the program and GUI up to choosing the two paths and them being displayed in the Entry boxes. However, I am running into an issue getting the output of the two paths from filedialog.askdirectory()
into the script that finds and moves the files.
This is what I have so far:
"Main file"
# Import required python modules
from tkinter import *
import tkinter as tk
import os
import shutil
from datetime import time
# Import custom module identifiers
import testfunc
import testgui
class ParentWindow(Frame):
def __init__(self, master, *args, **kwargs):
Frame.__init__(self, master, *args, **kwargs)
self.master = master
self.master.maxsize(700, 500)
self.master.minsize(700, 500)
self.master.title("Directory Auto-Sync®")
self.master.config(bg="#333")
self.master.protocol('WM_DELETE_WINDOW', lambda: testfunc.quit_message(self))
testfunc.center_window(self, 700, 500)
testgui.load_gui(self)
if __name__ == "__main__":
root = tk.Tk()
App = ParentWindow(root)
root.mainloop()
"GUI File"
# Import required python modules
from tkinter import *
import tkinter as tk
# Import custom module identifiers
import testfunc
import testmain
def load_gui(self):
self.src_lbl = tk.Label(
self.master,
font=("Helvetica", 16),
fg="#fff",
bg="#333",
text="Choose Source Directory",
)
self.src_lbl.grid(row=0, column=0, padx=(160, 0), pady=(50, 0), sticky=NSEW)
self.src_lbl.grid_rowconfigure(0, weight=1)
self.src_ent = tk.Entry(self.master, width=40)
self.src_ent.grid(row=1, column=0, padx=(160, 0), pady=(5, 0), sticky=NSEW)
self.src_ent.grid_rowconfigure(0, weight=1)
self.src_btn = tk.Button(
self.master,
highlightbackground="#333",
text="Browse",
command=lambda: testfunc.src_dir(self),
)
self.src_btn.grid(row=2, column=0, padx=(160, 0), pady=(10, 0), sticky=NSEW)
self.src_btn.grid_rowconfigure(0, weight=1)
self.dest_lbl = tk.Label(
self.master,
font=("Helvetica", 16),
fg="#fff",
bg="#333",
text="Choose Destination Directory",
)
self.dest_lbl.grid(row=3, column=0, padx=(160, 0), pady=(50, 0), sticky=NSEW)
self.dest_lbl.grid_rowconfigure(0, weight=1)
self.dest_ent = tk.Entry(self.master, width=40)
self.dest_ent.grid(row=4, column=0, padx=(160, 0), pady=(5, 0), sticky=NSEW)
self.dest_ent.grid_rowconfigure(0, weight=1)
self.dest_btn = tk.Button(
self.master,
highlightbackground="#333",
text="Browse",
command=lambda: testfunc.dest_dir(self),
)
self.dest_btn.grid(row=5, column=0, padx=(160, 0), pady=(10, 0), sticky=NSEW)
self.dest_btn.grid_rowconfigure(0, weight=1)
if __name__ == "__main__":
pass
"Function File"
# Import required python modules
import os
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
import sqlite3
# Import custom module identifiers
import testgui
import testmain
def center_window(self, w, h):
wscreen = self.master.winfo_screenwidth()
hscreen = self.master.winfo_screenheight()
x = int((wscreen / 2) - (700 / 2))
y = int((hscreen / 2) - (500 / 2))
cscreen = self.master.geometry("{}x{}+{}+{}".format(w, h, x, y))
return cscreen
def quit_message(self):
if messagebox.askokcancel("Directory Auto-Sync®", "Did you really want to quit Directory Auto-Sync®?"):
self.master.destroy()
sys.exit()._exit(0)
def src_dir(self):
dirpath = filedialog.askdirectory()
x = dirpath + "/"
self.src_ent.insert(END, x)
return
def dest_dir(self):
dir_path = filedialog.askdirectory()
x = dir_path + "/"
self.dest_ent.insert(END, x)
return
# def relay_source():
# src = var_s.get()
# return src
#
#
# def relay_dest():
# dest = var_d.get()
# return dest
if __name__ == '__main__':
pass
As you can see from the function file I am not sure how to proceed. I do have the following script that I know works if you enter in the directories manually (into the script itself), but not using the filedialog.askdirectory()
method described above.
Script to move files:
def current_milli_time():
x = int(round(time.time() * 1000))
return x
def move_files():
# s = var_s.get()
# source = os.listdir(s))
# x = '/'
# files = os.path.join(source, x)
# print(files)
# for i in files:
# paths = os.path.join(source, i)
# file_birth = os.stat(paths).st_birthtime
# file_modify = os.stat(paths).st_mtime
# if (current_milli_time() / 1000) - file_birth < 86400 or (
# current_milli_time() / 1000
# ) - file_modify < 86400:
# d = var_d + "/"
# shutil.copy2(paths, d)
# else:
# print("\n{} already in directory".format(paths))
# return
So as you can see I am linking the files with import modules since I'm not sure how I would do this all in one file, at least not yet. Also, I am new here and apologize for this being so long winded. I just wanted to give as much detail as possible. Thank you very much and I really look forward to what you guys think!