[Python] Pass text from watchdog to PyQT application

Discussão em 'Python' iniciado por Stack, Abril 1, 2025.

  1. Stack

    Stack Membro Participativo

    The task is to get a GUI application that will track file system changes and write a log in real time to a text widget. Currently, file system changes are output only to the console. Any ideas on how to send text from one class to another?

    class EventHandler(FileSystemEventHandler):

    def on_any_event(self, event: FileSystemEvent) -> None:
    '''pass event to plain_text widget PyQT app'''
    print(event)


    class MainWindow(QMainWindow):
    ...

    def set_text(self, text) -> None:
    self.plain_text.setPlainText(
    self.plain_text.toPlainText() + text + '\n'
    )

    def thread_watchdog(self) -> None:
    self.event_handler = EventHandler()
    self.observer = Observer()
    self.observer.schedule(self.event_handler, self.line_path_input.text(), recursive=False)
    self.observer.start()
    try:
    while True:
    if self.is_stop.is_set():
    break
    time.sleep(1)
    finally:
    self.observer.stop()
    self.observer.join()


    def run_watchdog(self) -> None:
    self.set_text('started')
    self.thread = Thread(target=self.thread_watchdog)
    self.thread.start()

    app = QApplication(sys.argv)
    window = MainWindow()
    app.exec_()

    Continue reading...

Compartilhe esta Página