【问题标题】:Update ListView dynamically when Log file updated更新日志文件时动态更新 ListView
【发布时间】:2020-05-19 11:04:29
【问题描述】:

我有一个 .txt 格式的日志文件,它会不断更新。然后我想在 ListView (PyQt5) 中动态显示文件内容

【问题讨论】:

    标签: pyqt5 python-3.7


    【解决方案1】:

    使用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

    【讨论】:

      猜你喜欢
      • 2016-10-06
      • 1970-01-01
      • 1970-01-01
      • 2012-07-29
      • 1970-01-01
      • 1970-01-01
      • 2018-08-04
      • 2018-01-19
      • 1970-01-01
      相关资源
      最近更新 更多