[PyQt] graphical file tail

Eric Frederich eric.frederich at gmail.com
Tue Jun 25 15:27:37 BST 2013


Thanks David,

This example works nicely.
It's nice to find out what you did wrong, but I'm always open to new
implementations.
In fact that is what I wanted, was someone to do it their way so I
could compare.
I'm sure I was doing something terribly wrong but didn't want to post
a "do it for me" email so I provided the implementation that I tried
;-)

Thanks,
~Eric


On Tue, Jun 25, 2013 at 8:22 AM, David Boddie <david.boddie at met.no> wrote:
> On Mon Jun 24 20:24:25 BST 2013, Eric Frederich wrote:
>
>> I'm trying to tail several files graphically.
>> I have been trying to find a way to tail several files in a GUI
>> without much luck at all.
>
> [...]
>
>> Basically, I want to graphically tail files and when the GUI closes
>> the tail subprocesses are killed.
>> Seems like a simple request, but I can't get it right.
>
> I notice that other messages have been talking about solving problems in
> your original implementation, but how about trying a different approach?
>
> The simple example below uses QProcess to show the output from a
> command. In this case, "tail -f <file>". It will stop the process when the
> window is closed, but not the process creating the file, of course.
>
> David
>
>
> #!/usr/bin/env python
>
> import sys
>
> from PyQt4.QtCore import *
> from PyQt4.QtGui import *
>
> class TailWidget(QTextBrowser):
>
>     def __init__(self, parent = None):
>         QTextBrowser.__init__(self, parent)
>         self.process = QProcess()
>         self.process.readyReadStandardOutput.connect(self.addStdout)
>         self.process.readyReadStandardError.connect(self.addStderr)
>         self.process.finished.connect(self.stop)
>         self.running = False
>
>     def start(self, command, arguments):
>         if self.running:
>             return
>
>         self.running = True
>         self.process.start(command, arguments)
>
>     def stop(self):
>         self.running = False
>
>     def addStdout(self):
>         self.append(QString.fromLocal8Bit(self.process.readAllStandardOutput()))
>
>     def addStderr(self):
>         self.append(QString.fromLocal8Bit(self.process.readAllStandardError()))
>
>     def closeEvent(self, event):
>         if self.running:
>             self.process.terminate()
>             self.process.waitForFinished(1000)
>
>         event.accept()
>
> if __name__ == "__main__":
>
>     app = QApplication(sys.argv)
>
>     if len(app.arguments()) != 2:
>         sys.stderr.write("Usage: %s <file>\n" % app.arguments()[0])
>         sys.exit(1)
>
>     w = TailWidget()
>     w.start("tail", ["-f", app.arguments()[1]])
>     w.show()
>     sys.exit(app.exec_())
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt


More information about the PyQt mailing list