# - *- coding:utf-8-*-
from Tkinter import *
import tkMessageBox
import tkFileDialog
import os
import fnmatch
def files():
text = entry1.get()
if not text:
tkMessageBox.showinfo("温馨提示:","请先查询关键词再选择文件夹")
return
fdn=tkFileDialog.askdirectory()#选择文件夹
fnlist =os.walk(fdn)#找到fdn这个文件夹里面的每个文件名(文件夹)
for roots,dirs,file in fnlist:
#roots=文件夹路径
#dirs=子文件名
#file=文件名
listbox.delete(0,END)#清空列表
for i in fnmatch.filter(file,entry2.get()):
fnpath=roots+"/"+i
filetext=open(fnpath).read()#打开文件获取字符串
if text in filetext:
listbox.insert(END,fnpath)
root=Tk()
root.title("搜索工具")
root.geometry("500x500+800+333")
Label(root,text="关键词:").grid() #从左往右布局,按表格方式布局
entry1=Entry(root)
entry1.grid(row=0,column=1)#row是行,column是列
Label(root,text="文件类型:").grid(row=0,column=2)#加文字输入框
entry2=Entry(root)
entry2.grid(row=0,column=3)#加输入框
button=Button(root,text="选择文件",fg="red",command=files)
button.grid(row=0,column=4)
listbox=Listbox(root,width=80)
listbox.grid(row=1,column=0,columnspan=5) #columspan是居中了
root.mainloop()
转载于:https://www.cnblogs.com/ZHANG576433951/p/6105990.html