【发布时间】:2020-05-19 11:04:29
【问题描述】:
我有一个 .txt 格式的日志文件,它会不断更新。然后我想在 ListView (PyQt5) 中动态显示文件内容
【问题讨论】:
标签: pyqt5 python-3.7
我有一个 .txt 格式的日志文件,它会不断更新。然后我想在 ListView (PyQt5) 中动态显示文件内容
【问题讨论】:
标签: pyqt5 python-3.7
使用QFileSystemWatcher 来检测您的文件何时被修改。
一个简单的例子:
class LogWatcher(QObject):
def __init__(self, parent=None):
super().__init__(parent)
self.watcher = QFileSystemWatcher()
self.watcher.addPath("./foobar.txt") # The file you want to check
self.watcher.fileChanged.connect(self.displayLine)
def displayLine(self, path): # Will be called when the file has changed
print(path, "has changed")
if __name__ == '__main__':
app = QtWidgets.QApplication([])
logger = LogWatcher()
sys.exit(app.exec_())
文件 foobar.txt 每次更改时,都会调用方法 displayLine
【讨论】: