faqts : Computers : Programming : Languages : Python : Snippets : Tkinter

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

12 of 14 people (86%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

Log file window (was: tail -f with Python/Tkinter)

Jul 5th, 2000 10:03
Nathan Wallace, unknown unknown, Hans Nowak, Snippet 384, Charles G Waldman


"""
Packages: gui.tkinter
"""
"""
 > 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()