'Sending output from watchdog when file is created with PyQt Signal and displaying that output in a PyQt window
Fairly new to PyQt and am close to giving up with this one. I have a watchdog class that creates output when a file is created, e.g. print "File Created". I want to display this output in a PyQt window. I previously had internal functions in the class creating PyQt messageboxes to display the text, but there were threading issues.
I'm trying to use signals to send the output to the MainWindow class but it just is not working at all. I know the code below has a lot of issues I just want to know where I'm going wrong. Much appreciated!
import os
import time
from PyQt5 import QtWidgets
from PyQt5.QtCore import pyqtSignal
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
results_file_formats = [".xls", ".xlsx"]
class Watcher:
output = pyqtSignal(str)
def __init__(self):
# Set the directory on watch
self.watch_path = "some/path"
self.observer = Observer()
self.event_handler = Handler()
self.observer.schedule(self.event_handler, self.watch_path, recursive=False)
self.observer.start()
self.output.connect(MainWindow().fileModified)
try:
while True:
time.sleep(5)
except Exception:
self.observer.stop()
self.observer.join()
def run(self):
pass
def modified(self):
self.output.emit(MainWindow().fileModified)
class Handler(FileSystemEventHandler):
output = pyqtSignal(str)
def __init__(self):
pass
def on_created(self, event):
if not event.is_directory and event.event_type == "created":
if "Results Folder" in os.path.dirname(event.src_path) and os.path.splitext(event.src_path)[1] in results_file_formats:
self.output = "File Created"
self.output.emit(MainWindow().fileModified)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
self.watcher = Watcher()
self.fileModified.connect("fileModified1")
self.show()
def fileModified(self):
print(1)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
window = MainWindow()
window.show()
app.exec_()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
