在搜索到文件路径后点击文件路径可以弹出新的子窗口并显示文件内容
def select_file():
print("点击搜索")
# 获取关键字和文件类型
keyword = keyword_entry.get()
if not keyword:
messagebox.showinfo(message="关键字")
return
file_type = type_entry.get()
if not type_entry:
messagebox.showinfo(message="文件类型")
return
# 获取文件夹路径
filepath = filedialog.askdirectory()
print("要搜索的路径为:{},关键字:{},文件类型:{}".format(filepath, keyword, filepath))
# 遍历并过滤文件夹,文件插入到列表中
result_list_box.delete(0, tk.END) # 插入前清空列表
for dir_path, dir_name, filenames in os.walk(filepath): # 文件夹路径 路径名 文件夹下的文件名
for filename in filenames:
_file = os.path.join(dir_path.replace('/', '\\'), filename)
if file_type and not _file.endswith(file_type):
continue
if keyword:
with open(_file, 'r', encoding='utf-8') as f:
if keyword not in f.read():
continue
result_list_box.insert(tk.END, _file)
print("end")
def create_scrollbar(master, target):
print(111)
sb = tk.Scrollbar(master, command=target.yview)
sb.pack(side=tk.RIGHT, fill=tk.Y)
target.config(yscrollcommand=sb.set)
def click_file(event):
print('选择文件')
# 获取列表框的文件路径
file_path = result_list_box.get(result_list_box.curselection()[0])
print('文件的路径', file_path)
# 新的窗口展示文件内容
top = tk.Toplevel()
top.title("查看内容")
top.iconbitmap('speed.ico')
# 文本框
file_text = tk.Text(top)
file_text.pack(side=tk.LEFT)
create_scrollbar(top, file_text)
with open(file_path, mode='r', encoding='utf-8-sig') as f: file_text.insert(tk.END, f.read())
result_list_box.bind('<Double-Button-1>', click_file)
root = tk.Tk()
root.geometry("600x300")
root.title("everytjing")
root.iconbitmap("./speed.ico")
# 搜索区域
search_frame = tk.Frame()
search_frame.pack()
# 关键字和文件类型
tk.Label(search_frame, text="关键字:").pack(side=tk.LEFT, padx=10)
keyword_entry = tk.Entry(search_frame)
keyword_entry.pack(side=tk.LEFT, padx=10)
tk.Label(search_frame, text="文件类型:").pack(side=tk.LEFT, padx=10)
type_entry = tk.Entry(search_frame)
type_entry.pack(side=tk.LEFT, padx=10)
# 搜索按钮
search_button = tk.Button(search_frame, text="搜索", command=select_file)
search_button.pack(side=tk.LEFT, padx=10)
result_list_box = tk.Listbox(root)
result_list_box.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
sb1 = tk.Scrollbar(root, command=result_list_box.yview)
sb1.pack(side=tk.RIGHT, fill=tk.Y)
result_list_box.config(yscrollcommand=sb1.set)
root.mainloop()然后下面是对上面代码进行封装,我就不分多个文件展示了,发一下最体现封装思想的app.py
import tkinter as tk
from tkinter import messagebox, filedialog
from utils import create_scrollbar, get_files, filter_files
from config import *
class Application(tk.Tk):
def __init__(self):
super().__init__()
# 窗口配置
self.title('everything')
self.geometry('600x400')
self.iconbitmap('speed.ico')
# 创建控件
self.search_frame = SerachFrame(self)
self.result_list_box = ResultListBox(self)
create_scrollbar(self, self.result_list_box)
# 绑定函数
self.search_frame.button_config_search(self.result_list_box)
self.result_list_box.bind_double_click()
class SearchFrame(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.pack(side=tk.TOP)
# 关键字的标签输入
tk.Label(self, text="关键字:").pack(side=tk.LEFT, padx=10)
self.keyword_entry = tk.Entry(self)
self.keyword_entry.pack(side=tk.LEFT, padx=10)
# 文件类型的标签和输入
tk.Label(self, text="文件类型:").pack(side=tk.LEFT, padx=10)
self.type_entry = tk.Entry(self)
self.type_entry.pack(side=tk.LEFT, padx=10)
# 搜索按钮
self.search_button = tk.Button(self, text="搜索")
self.search_button.pack(side=tk.LEFT, padx=10)
def button_config_search(self, result_list_box):
self.search_button.config(command=self.search(result_list_box))
def get_search_data(self):
keyword = self.keyword_entry.get()
if not keyword:
messagebox.showinfo(message="关键字")
return
file_type = self.type_entry.get()
if not file_type:
messagebox.showinfo(message="文件类型")
return
# 获取文件夹路径
filepath = filedialog.askdirectory()
print(f"要搜索的路径为:{keyword},关键字:{file_type},文件类型:{filepath}")
return {
"keyword": keyword,
"filetype": file_type,
"filepath": filepath,
}
def search(self, result_list_box):
def func():
print('开始搜索文件')
search_data = self.get_search_data()
if not search_data:
return
files = filter_files(
files=get_files(search_data["filepath"]),
filetype=search_data["file_type"],
ketyword=search_data["keyword"]
)
# 文件名填入列表框
result_list_box.delete(0, tk.END)
result_list_box.insert(tk.END, *files)
print("文件插入到列表框完成")
return func
class ViewWindow(tk.Toplevel):
def __init__(self):
super().__init__()
self.title("查看内容")
self.iconbitmap('speed.ico')
self.file_text = tk.Text(self)
self.file_text.pack(side=tk.LEFT)
create_scrollbar(self, self.file_text)
def insert_file(self, file_path):
print("读取文件内容,并插入到文本框")
with open(file_path, 'r', encoding='utf-8') as f: self.file_text.insert(tk.END, f.read())
print("插入完毕")
class ResultListBox(tk.Listbox):
def __init__(self, master):
super().__init__(master)
self.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
def bind_double_click(self):
self.bind('<Double-Button-1>', self.click_file)
def click_file(self, event):
print("点击选择文件")
# 获取列表框被选中的文件路径
file_path = self.get(self.curselection()[0])
print('文件路径', file_path)
# 新的窗口展示文件内容
ViewWindow().insert_file(file_path)
该代码实现了一个基于Tkinter的图形用户界面,允许用户输入关键字和文件类型来搜索特定目录中的文件。搜索结果会显示在一个列表框中,用户双击文件可在一个新窗口中查看文件内容。

被折叠的 条评论
为什么被折叠?



