python做的本地文件搜索工具

解决每次打开Everything都需要等待更新索引的问题

from subprocess import Popen
from tkinter import Tk, Entry, ttk, Menu
from ctypes import WinDLL, create_unicode_buffer
 
 
def on_right_click(event):
    item = tree.identify_row(event.y)
    menu = Menu(root, tearoff=0)
    menu.add_command(label="打开目录", command=lambda: Popen(f'explorer "{tree.item(item, "values")[1]}\\"'))
    menu.post(event.x_root, event.y_root)
 
 
def search(_):
    query = search_entry.get()
    everything_dll.Everything_SetSearchW(query)
    everything_dll.Everything_QueryW(True)
 
    num_results = everything_dll.Everything_GetNumResults()
    root.title(f'找到 {num_results} 条结果')
 
    for item in tree.get_children():
        tree.delete(item)
 
    for i in range(min(num_results, max_results)):
        buffer = create_unicode_buffer(buffer_size)
        everything_dll.Everything_GetResultFullPathNameW(i, buffer, buffer_size)
        path, name = buffer.value.rsplit("\\", 1)
        tree.insert("", "end", values=(name, path))
 
 
everything_dll = WinDLL('Everything64.dll')
 
max_results = 50
buffer_size = 260
 
root = Tk()
root.attributes('-alpha', 0.98)
 
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
window_width = 1400
window_height = 650
x = (screen_width - window_width) // 2
y = (screen_height - window_height) // 2
root.geometry(f"{window_width}x{window_height}+{x}+{y}")
 
root.columnconfigure(0, weight=1)
root.rowconfigure(1, weight=1)
 
search_entry = Entry(root)
search_entry.grid(sticky="ew")
search_entry.bind("<KeyRelease>", search)
 
columns = ("name", "path")
tree = ttk.Treeview(root, columns=columns, show='headings')
tree.grid(sticky="nsew")
tree.heading("name", text="名称", anchor="w")
tree.heading("path", text="路径", anchor="w")
tree.bind("<Button-3>", on_right_click)
 
root.mainloop()
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DNCS高级工程师

你的创作是我打赏的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值