0

So in short what im trying to do is to have an arduino with leds and a gui display the information stored within a continiously updating json file(its from a game called Elite Dangerous).

So im using the watchdog to see when the file is updated and then read the new information. I would then like to use tkinter to display the information as for example: boxes changing color

However the problem is that when i use tkinter the mainloop() stops the rest of the code from running (im assuming this is because its an infinite loop). Im very new to programming still so please be genlte :P Im quite new to this site as well so please tell me if any information is missing

def main(file_path=None):
    watched_dir = os.path.split(file_path)[0]
    print ('watched_dir = {watched_dir}'.format(watched_dir=watched_dir)) #prints the directory
    patterns = [file_path]
    print ('patterns = {patterns}'.format(patterns=','.join(patterns))) #prints the file with path
    event_handler = MyEventHandler(patterns=patterns)

#creating the observer and watching the file
    observer = Observer()
    observer.schedule(event_handler, watched_dir, recursive=True)
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
        observer.join()
Robin BT
  • 3
  • 2
  • You can do this, but you have to be a little bit carefully. You need to seperate the GUI and your programm from eachother with threading. You can make your GUI read a file but you cant make your GUI updated by json. You may are intrested in this https://stackoverflow.com/questions/26703502/threads-and-tkinter/26703844#26703844 – Thingamabobs Aug 02 '20 at 16:33
  • oki i will try to look into it when i get home :) – Robin BT Aug 02 '20 at 16:42
  • 1
    Just to be aware of it. I wrote one small program yet with tkinter and threading and I hated it. It can take some time to work this out. Best wishes! – Thingamabobs Aug 02 '20 at 16:45
  • `tkinter` and `watchdog` (Is it the [watchdog](https://pypi.org/project/watchdog/) in pypi?) can be run together because `watchdog` is actually running as thread. – acw1668 Aug 02 '20 at 17:25
  • it is the pypi watchdog yes. Im sorry but how would i go about making the tkinter run alongside the watchdog ? ive also tried to make the gui an own thread as stated in the comment above and it still stops the rest of the code – Robin BT Aug 02 '20 at 17:37
  • tkinter should be run in main thread. How do you run `watchdog`? – acw1668 Aug 02 '20 at 17:43
  • updated the question with the code for the watchdog but it should just run from the main? sorry still new to this – Robin BT Aug 02 '20 at 17:49
  • You can remove the `try/except` block. Call `observer.stop()` and `observer.join()` after tkinter `mainloop()`. – acw1668 Aug 02 '20 at 17:57

1 Answers1

2

Below is an example on running tkinter and watchdog together:

import tkinter as tk
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

class MyEventHandler(FileSystemEventHandler):
    def __init__(self, callback):
        super().__init__()
        self.callback = callback

    def on_modified(self, event):
        self.callback(event.src_path)

def on_modified(target):
    txtbox.insert('end', target+' is modified\n')

event_handler = MyEventHandler(on_modified)
observer = Observer()
observer.schedule(event_handler, '.', recursive=False)

root = tk.Tk()

txtbox = tk.Text(root, width=40, height=20)
txtbox.pack()

observer.start()
root.mainloop()

observer.stop()
observer.join()
acw1668
  • 40,144
  • 5
  • 22
  • 34