Python下用tkinter实现tail -f功能,任何一个Python应用都可以调用

啥也不说了,直接上代码:

#用法:见if __name__ == '__main__'
#作者:陈福明博士
import os
from tkinter import *
class Tail(object):
    def __init__(self, tailed_file):
        self.check_file_validity(tailed_file)
        self.tailed_file = tailed_file
        self.curr_position = 0
        self.fp = None
        self.timer =None
        self.root = None
    def open(self):
        root=Tk()
        self.root = root
        root.title('日志跟踪窗口')#窗口名字
        root.geometry('960x600')#设置主窗口大小
        self.text = Text(root, height=550)
        self.text.pack(fill=BOTH)
        self.follow()
        #root.after(10000, self.showline) # 这里的10000单位为毫秒
        root.protocol("WM_DELETE_WINDOW", self.close) #退出或者关闭窗口的时候调用close
        root.mainloop()
    def __del__(self): #覆盖退出函数
        self.close()
    def close(self):
        print('exit Tail')
        if self.fp != None:
            self.fp.close()
            self.fp = None
        if self.timer != None:
            self.timer.cancel()
            self.timer = None
        if self.root != None:
            self.root.destroy()
            self.root.quit()
            self.root = None
    def showline(self):
        print('call showline') #调试信息,可以注释掉
        try:
            fp = self.fp
            self.curr_position = fp.tell()
            line = fp.readline()
            print(line)
            if not line:
                fp.seek(self.curr_position)
            else:
                self.text.insert("end", line) # self.callback(line)
            import threading
            self.timer = threading.Timer(1, self.showline)
            self.timer.start()
        except:
            print('showline exception!')
    def follow(self):
        #with open(self.tailed_file) as file_:
        self.fp = open(self.tailed_file,'r')
        self.fp.seek(0,2)
        #self.fp = file_
        import threading
        self.timer = threading.Timer(1, self.showline)
        self.timer.start()
    def check_file_validity(self, file_):
        if not os.access(file_, os.F_OK):
            raise TailError("File '%s' does not exist" % (file_))
        if not os.access(file_, os.R_OK):
            raise TailError("File '%s' not readable" % (file_))
        if os.path.isdir(file_):
            raise TailError("File '%s' is a directory" % (file_))
class TailError(Exception):
    def __init__(self, msg):
        self.message = msg
    def __str__(self):
        return self.message
if __name__ == '__main__':
    ROOT_PATH = 'tkdata.log' #你的日志文件
    t = Tail(ROOT_PATH) #外部调用时,记得保存对象变量t,用于调用结束后关闭窗口
    t.open() #打开窗口    
    #t.close() #外部调用的时候,使用完之后,记得调用Tail类的close()函数主动关闭这个窗口
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值