关键字文本过滤器,关键字过滤器,大小写不敏感,可以保存筛


大小写不敏感,可以保存筛选结果

from tkinter import *import tkinter.filedialogfrom datetime import datetime####################################################filepath = ''####################################################def open_file():    global filepath    filepath = tkinter.filedialog.askopenfilename(initialdir = 'C:/', \\                                                  filetypes=[("txt files", "*.txt")])####################################################def on_click():    global filepath    #clear list box    list.delete(0, END)    #get check button value    save2file = CheckVar.get()    if not filepath:        label['text'] = "Please select file first!"        return    else:        sKeyWord = tText.get()        if sKeyWord == '':            label['text'] = "Please input words!"            return        label['text'] = "search:" +  sKeyWord        sKeyWord = sKeyWord.lower()    #remove space    filepath = filepath.strip()    #find the last /    filename_num = filepath.rfind('/') + 1    #filename = filepath[filename_num : ]    src_ofp = open(filepath, "r")    if save2file:        dest_filepath = filepath[0 : filename_num]        now = datetime.now().strftime('%y%m%d_%H%M%S')        dest_filepath = dest_filepath + 'KW' + sKeyWord + \\                        '_' + now + '.txt'        dest_ofp = open(dest_filepath, "w")    #separate words by space/tab...    lKeyWord = sKeyWord.split()    KeyWordsNum = len(lKeyWord)    line_num = 0    list_idx = 0    for line in src_ofp.readlines():        line_lower = line.lower()        line_num += 1        i = 0        for i in range(0, KeyWordsNum):            if line_lower.find(lKeyWord[i]) == -1:                continue            else:                i = KeyWordsNum            line = str(line_num) + ' : ' + line            list.insert(list_idx, line)            list_idx += 1            if save2file:                dest_ofp.write(line)    src_ofp.close()    if save2file:        dest_ofp.flush()        dest_ofp.close()    label['text'] = "Done"####################################################root = Tk(className = 'LogKeyWordFilter V1')root.wm_minsize(640, 480)root.wm_maxsize(640, 480)label = Label(root)label['text'] = 'Default search \\'error\\' words'label.pack()label.place(x = 400, y = 5)tText = StringVar()tText.set('error')entry = Entry(root)entry['textvariable'] = tTextentry.pack()entry.place(x = 160, y = 5, width = 220)CheckVar = IntVar()Cbutton = Checkbutton(root, text = "Save to File", variable = CheckVar, \\                      onvalue = 1, offvalue = 0, height=0, \\                      width = 20)Cbutton.pack()Cbutton.place(x = 0, y = 5)button = Button(root, height = 2, width = 6)button['text'] = 'Search'button['command'] = on_clickbutton.pack()button.place(x = 200, y = 30)button1 = Button(root, height = 2, width = 6)button1['text'] = 'Open'button1['command'] = open_filebutton1.pack()button1.place(x = 400, y = 30)yscrollbar = Scrollbar(root, orient='vertical')yscrollbar.pack(side = RIGHT, fill=Y)xscrollbar = Scrollbar(root, orient='horizontal')xscrollbar.pack(side = BOTTOM, fill=X)list = Listbox(root, yscrollcommand = yscrollbar.set, \\               xscrollcommand = xscrollbar.set, height = 30, width = 103)list.pack(side = LEFT, fill = BOTH, expand = True)list.place(x = 0, y = 70)#x,y view bounding to list boxxscrollbar.config(command = list.xview)yscrollbar.config(command = list.yview)root.mainloop()#该片段来自于http://byrx.net

评论关闭