tail -f with Python/Tkinter
Charles G Waldman
cgw at fnal.gov
Thu Dec 9 15:27:55 EST 1999
pstmarie at my-deja.com writes:
> Hi,
> I'm quite new to Python and have not been able to
> find the answer to this in my books or on the web.
> How do I tail -f a log file using a Tkinter
> widget? It's on a Linux system..
I'm not sure if you mean you really want to run "tail -f" as a child
process and collect its output, or just do the equivalent thing in
Python. The latter is much simpler - "tail" is a very simple program
and it's easier to just do the equivalent thing in Python, rather than
opening a pipe to a child process.
Here's a simple script which may do what you need:
#!/usr/bin/env python
import sys,os
import time
from Tkinter import *
from ScrolledText import ScrolledText
class LogViewer(Frame):
def __init__(self, parent, filename):
Frame.__init__(self,parent)
self.filename = filename
self.file = open(filename, 'r')
self.text = ScrolledText(parent)
self.text.pack(fill=BOTH)
data = self.file.read()
self.size = len(data)
self.text.insert(END, data)
self.after(100, self.poll)
def poll(self):
if os.path.getsize(self.filename) > self.size:
data = self.file.read()
self.size = self.size + len(data)
self.text.insert(END, data)
self.after(100,self.poll)
if __name__ == "__main__":
root = Tk()
viewer = LogViewer(root, sys.argv[1])
viewer.mainloop()
More information about the Python-list
mailing list