2

I am trying this now for almost 24 hours, but I just can't get it to work and am not finding examples/solutions suitable for my use-case..

What I am trying to do is:

  • Watch a directory with watchdog on file changes
  • on on_modified event open a tkinter GUI

Problem: Main-Thread issues with the mainloop() and I only find examples for scripts where the GUI is always rendered straight from the beginning ..

Error: WARNING: NSWindow drag regions should only be invalidated on the Main Thread! or main thread is not in main loop tkinter

Minimalist code example:

class PopupWindow:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)

        # code for input fields
        # ...

        self.frame.pack()

    def set_user_input(self):
        self.master.destroy()
        self.master.quit()


class MainClass(FileSystemEventHandler):
    def __init__(self):
        print("Watching files in '/directory-to-be-watched' for changes...")

    def run(self):
        print("Starting script")

        root = tk.Tk()
        app = PopupWindow(root)
        # read input values ...
        root.mainloop()
        root.destroy()

    def on_modified(self, event):
        self.run()


event_handler = MainClass()
observer = Observer()
observer.schedule(event_handler, "/directory-to-be-watched", recursive=False)
observer.start()


try:
    while True:
        time.sleep(10)

except KeyboardInterrupt:
    observer.stop()

observer.join()

ANY help would be highly appreciated, thanks in advance!

davbuc
  • 537
  • 6
  • 14
  • 1
    You have to run either `tkinter` **OR** the `Observer` in a **own** `Thread`. – stovfl Feb 22 '20 at 21:12
  • @stovfl thanks for the reply! Could you roughly explain how I do that? – davbuc Feb 22 '20 at 21:51
  • Follow this approach [use threads to preventing main event loop from “freezing”](https://stackoverflow.com/a/16747734/7414759) – stovfl Feb 22 '20 at 21:54

0 Answers0