watchdog(Python';s库)-当文件被修改时如何发送信号?
检测特定文件何时被修改的类的代码:watchdog(Python';s库)-当文件被修改时如何发送信号?,python,pyqt,watchdog,python-watchdog,Python,Pyqt,Watchdog,Python Watchdog,检测特定文件何时被修改的类的代码: class MyEventHandler(FileSystemEventHandler, QtCore.QThread): def __init__(self, filename): super(MyEventHandler, self).__init__() self.filename = filename def on_modified(self, event): if not event.
class MyEventHandler(FileSystemEventHandler, QtCore.QThread):
def __init__(self, filename):
super(MyEventHandler, self).__init__()
self.filename = filename
def on_modified(self, event):
if not event.is_directory and event.src_path.endswith(self.filename):
print "modified"
self.emit(QtCore.SIGNAL("fileModified"))
class WatchOutForFileModifications(QtCore.QThread):
def __init__(self, path, filename):
super(WatchOutForFileModifications, self).__init__()
self.path = path
self.filename = filename
self.observer = Observer()
self.event_handler = MyEventHandler(self.filename)
self.observer.schedule(self.event_handler, self.path, recursive=False)
self.observer.start()
def run(self):
while 1:
self.connect(self.event_handler, QtCore.SIGNAL("fileModified"), self.modified)
def modified(self):
self.emit(QtCore.SIGNAL("fileModified1"))
以及应用程序本身的代码段:
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
uic.loadUi('test.ui', self)
path = "somePath"
filename = "someName"
self.fileWatcher = WatchOutForFileModifications(path, filename)
self.fileWatcher.start()
self.connect(self.fileWatcher, QtCore.SIGNAL("fileModified1"), self.fileModified)
self.show()
def fileModified(self):
print 1
问题是,当一个文件被修改时,我得到一个不间断的流1正在打印。我意识到在WatchOutForFileModifications
类中不应该以这种方式发出/连接信号,但我不理解API:--应该如何工作。至少我假设这是我应该用来监听文件修改的API
编辑
经过一些修改后的工作代码:
import sys
from PyQt4 import QtGui, QtCore, uic
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
class MyEventHandler(FileSystemEventHandler, QtCore.QThread):
def __init__(self, filename):
super(MyEventHandler, self).__init__()
self.filename = filename
self.signalName = str(filename) + "_modified"
def on_modified(self, event):
if not event.is_directory and event.src_path.endswith(self.filename):
self.emit(QtCore.SIGNAL(self.signalName))
class FileModificationWatcher(QtCore.QThread):
def __init__(self, path, filename):
super(FileModificationWatcher, self).__init__()
self.path = path
self.filename = filename
self.observer = Observer()
self.event_handler = MyEventHandler(self.filename)
self.observer.schedule(self.event_handler, self.path, recursive=False)
self.observer.start()
def run(self):
pass
def getEmitter(self):
return self.event_handler
def getSignalName(self):
return self.event_handler.signalName
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
uic.loadUi('test.ui', self)
path = "somePath"
filename = "someName"
self.fileWatcher = FileModificationWatcher(path, filename)
self.fileWatcher.start()
self.connect(self.fileWatcher.getEmitter(), QtCore.SIGNAL(self.fileWatcher.getSignalName()), self.fileModified)
self.show()
def fileModified(self):
print 1
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
在您的WatchOutForFileModifications类中,尝试在init方法中连接信号。在您的WatchOutForFileModifications类中,尝试使用init方法连接信号。问题在于,在
WatchOutForFileModifications
中,您重复将信号连接到run函数中的插槽。要解决您遇到的问题,请调用self.connect
并将其移动到类的\uuuuu init\uuuuu
,如下所示:
from PyQt4 import QtCore, QtGui
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
class MyEventHandler(FileSystemEventHandler, QtCore.QThread):
def __init__(self, filename):
super(MyEventHandler, self).__init__()
self.filename = filename
def on_modified(self, event):
if not event.is_directory and event.src_path.endswith(self.filename):
print("modified")
self.emit(QtCore.SIGNAL("fileModified"))
class WatchOutForFileModifications(QtCore.QThread):
def __init__(self, path, filename):
super(WatchOutForFileModifications, self).__init__()
self.path = path
self.filename = filename
self.observer = Observer()
self.event_handler = MyEventHandler(self.filename)
self.observer.schedule(self.event_handler, self.path, recursive=False)
self.observer.start()
self.connect(self.event_handler, QtCore.SIGNAL("fileModified"), self.modified)
def run(self):
pass
def modified(self):
self.emit(QtCore.SIGNAL("fileModified1"))
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
path = r'D:\Code\\'
filename = "Hexagon_Grid_Creation.py"
self.fileWatcher = WatchOutForFileModifications(path, filename)
#self.fileWatcher.start()
self.connect(self.fileWatcher, QtCore.SIGNAL("fileModified1"), self.fileModified)
self.show()
def fileModified(self):
print(1)
if __name__ == '__main__':
app = QtGui.QApplication([])
window = MainWindow()
window.show()
app.exec_()
在这种情况下,您可能不需要
QThread
;事件处理程序正在监视该文件,因此您实际上不需要在后台运行任何其他内容。我认为您可以完全取消该类,只需在主窗口中实例化事件处理程序。问题是在WatchOutForFileModifications
中,您重复将信号连接到run函数中的插槽。要解决您遇到的问题,请调用self.connect
并将其移动到类的\uuuuu init\uuuuu
,如下所示:
from PyQt4 import QtCore, QtGui
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
class MyEventHandler(FileSystemEventHandler, QtCore.QThread):
def __init__(self, filename):
super(MyEventHandler, self).__init__()
self.filename = filename
def on_modified(self, event):
if not event.is_directory and event.src_path.endswith(self.filename):
print("modified")
self.emit(QtCore.SIGNAL("fileModified"))
class WatchOutForFileModifications(QtCore.QThread):
def __init__(self, path, filename):
super(WatchOutForFileModifications, self).__init__()
self.path = path
self.filename = filename
self.observer = Observer()
self.event_handler = MyEventHandler(self.filename)
self.observer.schedule(self.event_handler, self.path, recursive=False)
self.observer.start()
self.connect(self.event_handler, QtCore.SIGNAL("fileModified"), self.modified)
def run(self):
pass
def modified(self):
self.emit(QtCore.SIGNAL("fileModified1"))
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
path = r'D:\Code\\'
filename = "Hexagon_Grid_Creation.py"
self.fileWatcher = WatchOutForFileModifications(path, filename)
#self.fileWatcher.start()
self.connect(self.fileWatcher, QtCore.SIGNAL("fileModified1"), self.fileModified)
self.show()
def fileModified(self):
print(1)
if __name__ == '__main__':
app = QtGui.QApplication([])
window = MainWindow()
window.show()
app.exec_()
在这种情况下,您可能不需要QThread
;事件处理程序正在监视该文件,因此您实际上不需要在后台运行任何其他内容。我认为您可以完全取消该类,只需在main窗口中实例化事件处理程序。我实际上意识到我可以直接从MyEventHandler
连接到main窗口
,完全跳过WatchOutForFileModifications
中的信号处理。我实际上意识到我可以直接从MyEventHandler
连接到main窗口
,完全跳过WatchOutForFileModifications
中的信号处理。