2

I'm trying the following

  • Create a GUI with tkinter where the user will choose a directory to watch
  • Close the window
  • Pass the path to the directory to watchdog so it can watch for file changes

How does one go about combining both scripts into one app ?

This below post has a script which does nothing when I add a *.jpg file to my temp folder (osx). https://stackoverflow.com/a/41684432/11184726

Can someone point me towards a course or tutorial that will help me understand how to combine whats going on.

1. GUI :

from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter import filedialog

from tkinter.messagebox import showerror

globalPath = ""

class MyFrame(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.master.title("Example")
        self.master.rowconfigure(5, weight=1)
        self.master.columnconfigure(5, weight=1)
        self.grid(sticky=W+E+N+S)

        self.button = Button(self, text="Browse", command=self.load_file, width=10)
        self.button.grid(row=1, column=0, sticky=W)

    def load_file(self):

        fname = askopenfilename(filetypes=(("Text File", "*.txt"),("All files", "*.*") ))
        global globalPath
        globalPath = fname
#         fname = askopenfilename(filetypes=(("Text File", "*.txt"),("All files", "*.*") ))
        if fname:
            try:
                print("""here it comes: self.settings["template"].set(fname)""")
                print (fname)
            except:                     # <- naked except is a bad idea
                showerror("Open Source File", "Failed to read file\n'%s'" % fname)
            return

if __name__ == "__main__":

    MyFrame().mainloop() # All above code will run inside window
print(__name__)
print("the path to the file is : " + globalPath)

2. Watchdog :

from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler

def on_created(event):
    # This function is called when a file is created
    print(f"hey, {event.src_path} has been created!")

def on_deleted(event):
    # This function is called when a file is deleted
    print(f"what the f**k! Someone deleted {event.src_path}!")

def on_modified(event):
    # This function is called when a file is modified
    print(f"hey buddy, {event.src_path} has been modified")
    #placeFile() #RUN THE FTP


def on_moved(event):
    # This function is called when a file is moved    
    print(f"ok ok ok, someone moved {event.src_path} to {event.dest_path}")




if __name__ == "__main__":



    # Create an event handler
    patterns = "*" #watch for only these file types "*" = any file
    ignore_patterns = ""
    ignore_directories = False
    case_sensitive = True

    # Define what to do when some change occurs 
    my_event_handler = PatternMatchingEventHandler(patterns, ignore_patterns, ignore_directories, case_sensitive)
    my_event_handler.on_created = on_created
    my_event_handler.on_deleted = on_deleted
    my_event_handler.on_modified = on_modified
    my_event_handler.on_moved = on_moved

    # Create an observer
    path = "."
    go_recursively = False # Will NOT scan sub directories for changes 

    my_observer = Observer()
    my_observer.schedule(my_event_handler, path, recursive=go_recursively)

    # Start the observer
    my_observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        my_observer.stop()
    my_observer.join() 
4

1 に答える 1