一、效果图

二、代码

miniSearch.py

from tkinter import *
from tkinter import ttk, messagebox, filedialog
from threading import Thread
import os
import queue
import loadingDialog
import re class Application_UI(object):
# 默认查找路径
search_path = os.path.abspath("./")
# 是否开始查找标志
is_start = 0 def __init__(self):
# 设置UI界面
self.window = Tk()
win_width = 600
win_height = 500
screen_width = self.window.winfo_screenwidth()
screen_height = self.window.winfo_screenheight()
x = int((screen_width - win_width) / 2)
y = int((screen_height - win_height) / 2)
self.window.title("磁盘文件搜索工具")
self.window.geometry("%sx%s+%s+%s" % (win_width, win_height, x, y))
# 最好用绝对路径
self.window.iconbitmap(r"G:\PyCharm 2019.1\project\TK\项目\磁盘搜索工具\icon.ico")
# top_frame = Frame(self.window)
top_frame.pack(side = TOP, padx = 5, pady = 20, fill = X) self.search_val = StringVar()
entry = Entry(top_frame, textvariable = self.search_val)
entry.pack(side = LEFT, expand = True, fill = X, ipady = 4.5) search_btn = Button(top_frame, text = "搜索", width = 10, command = self.search_file)
search_btn.pack(padx = 10) bottom_frame = Frame(self.window) bottom_frame.pack(side = LEFT, expand = True, fill = BOTH, padx = 5) tree = ttk.Treeview(bottom_frame, show = "headings", columns = ("name", "path"))
self.treeView = tree
y_scroll = Scrollbar(bottom_frame)
y_scroll.config(command = tree.yview)
y_scroll.pack(side = RIGHT, fill = Y)
x_scroll = Scrollbar(bottom_frame)
x_scroll.config(command = tree.yview, orient = HORIZONTAL)
x_scroll.pack(side = BOTTOM, fill = X)
tree.config(xscrollcommand = x_scroll.set, yscrollcommand = y_scroll.set) tree.column("name", anchor = "w", width = 8)
tree.column("path", anchor = "w")
tree.heading("name", text = "文件名称", anchor = "w")
tree.heading("path", text = "路径", anchor = "w")
tree.pack(side = LEFT, fill = BOTH, expand = True, ipady = 20) menu = Menu(self.window)
self.window.config(menu = menu) set_path = Menu(menu, tearoff = 0)
set_path.add_command(label = "设置路径", accelerator="Ctrl + F", command = self.open_dir)
set_path.add_command(label = "开始扫描", accelerator="Ctrl + T", command = self.search_file) menu.add_cascade(label = "文件", menu = set_path) about = Menu(menu, tearoff = 0)
about.add_command(label = "版本", accelerator = "v1.0.0")
about.add_command(label = "作者", accelerator = "样子")
menu.add_cascade(label = "关于", menu = about) self.progressbar = loadingDialog.progressbar()
# 设置队列,保存查找完毕标志
self.queue = queue.Queue()
# 开始监听进度条
self.listen_progressBar() self.window.bind("<Control-Key-f>", lambda event: self.open_dir())
self.window.bind("<Control-Key-r>", lambda event: self.search_file())
self.window.protocol("WM_DELETE_WINDOW", self.call_close_window)
self.window.mainloop() class Application(Application_UI):
def __init__(self):
Application_UI.__init__(self) def call_close_window(self):
self.progressbar.exit_()
self.window.destroy() ''' 监听进度条'''
def listen_progressBar(self):
# 窗口每隔一段时间执行一个函数
self.window.after(400, self.listen_progressBar)
while not self.queue.empty():
queue_data = self.queue.get()
if queue_data == 1:
# 关闭进度条
self.progressbar.exit_()
self.is_start = 0 ''' 设置默认搜索路径'''
def open_dir(self):
path = filedialog.askdirectory(title = u"设置目录", initialdir = self.search_path)
print("设置路径:"+path)
self.search_path = path ''' 开始搜索'''
def search_file(self):
def scan(self, keyword):
# 清空表格数据
for _ in map(self.treeView.delete, self.treeView.get_children()):
pass # 筛选文件
self.find_file_insert(keyword) # 设置查找完毕标志
self.queue.put(1) if self.is_start == 0:
# 获取查找关键词
keyword = str.strip(self.search_val.get())
if not keyword:
messagebox.showerror("提示", "请输入文件名称")
self.search_val.set("")
return # 设置已经开始查找状态
self.is_start = 1
# 开启线程
self.thread = Thread(target = scan, args = (self, keyword))
self.thread.setDaemon(True)
self.thread.start() # 显示进度条
self.progressbar.start()
else:
pass ''' 查找设置目录下所有文件并插入表格'''
def find_file_insert(self, keyword):
try:
for root, dirs, files in os.walk(self.search_path, topdown = True):
for file in files:
match_result = self.file_match(file, keyword)
if match_result is True:
file_path = os.path.join(root, file)
# 插入数据到表格
self.treeView.insert('', END, values = (file, file_path))
# 更新表格
self.treeView.update()
except Exception as e:
print(e) return True ''' 名称匹配'''
def file_match(self, file, keyword):
print("文件匹配:",keyword, file)
result = re.search(r'(.*)'+keyword+'(.*)', file, re.I)
if result:
return True
return False if __name__ == "__main__":
Application()

loadingDialog.py

。。。

有需要这个文件的可以评论联系我哦

有兴趣的可以做磁盘文件内容查找功能:

1、获取磁盘下所有文件

2、打开文件进行正则查找,找到匹配的放入一个列表

3、用TreeView展示文件地址等信息

Tkinter 之磁盘搜索工具实战的更多相关文章

  1. python实现文件搜索工具(简易版)

    在python学习过程中有一次需要进行GUI 的绘制, 而在python中有自带的库tkinter可以用来简单的GUI编写,于是转而学习tkinter库的使用. 学以致用,现在试着编写一个简单的磁文件 ...

  2. 揭开Faiss的面纱 探究Facebook相似性搜索工具的原理

    https://www.leiphone.com/news/201703/84gDbSOgJcxiC3DW.html 本月初雷锋网报道,Facebook 开源了 AI 相似性搜索工具 Faiss.而在 ...

  3. 8.1 fdisk:磁盘分区工具

    fdisk 是Linux下常用的磁盘分区工具.受mbr分区表的限制,fdisk工具只能给小于2TB的磁盘划分分区.如果使用fdisk对大于2TB的磁盘进行分区,虽然可以分区,但其仅识别2TB的空间,所 ...

  4. ElasticSearch7.X.X-初见-模仿京东搜索的实战

    目录 简介 聊聊Doug Cutting ES&Solr&Lucene ES的安装 安装可视化界面ES head插件 了解ELK 安装Kibana ES核心概念 文档 类型 索引 倒排 ...

  5. 用python制作文件搜索工具,深挖电脑里的【学习大全】

    咳咳~懂得都懂啊 点击此处找管理员小姐姐领取正经资料~ 开发环境 解释器: Python 3.8.8 | Anaconda, Inc. 编辑器: pycharm 专业版 先演示效果 开始代码,先导入模 ...

  6. 如何在Windows Server 2008 R2没有磁盘清理工具的情况下使用系统提供的磁盘清理工具

    今天,刚好碰到服务器C盘空间满的情况,首先处理了临时文件和有关的日志文件后空间还是不够用,我知道清理C盘的方法有很多,但今天只分享一下如何在Windows Server 2008 R2没有磁盘清理工具 ...

  7. centos locate搜索工具

    locate搜索工具 [root@localhost ~]# yum install mlocate [root@localhost ~]# locate passwd locate: can not ...

  8. FileSeek文件内容搜索工具下载

    Windows 内建的搜索功能十分简单,往往不能满足用户的需要.很多的第三方搜索工具因此诞生,比如 Everything,Locate32等. 而FileSeek也是一款不错的搜索工具,其不同于其他搜 ...

  9. 命令行的全文搜索工具--ack

    想必大家在命令行环境下工作时候,一定有想要查找当前目录下的源代码文件中的某些字符的需求,这时候如果使用传统方案,你可能需要输入一长串的命令,比如这样: 1. grep -R 'string' dir/ ...

随机推荐

  1. springMVC + mybatis 下出现JDBC Connection *** will not be managed by Spring错误

    仔细查看配置中是否有如下类似的配置 execution(* com.ciguo.service.*.*(..)) <aop:config> <aop:pointcut id=&quo ...

  2. Oracle 创建数据表

    数据库中的每一个表都被一个模式(或用户)所拥有,因此表是一种典型的模式对象.在创建数据表时,Oracle 将在一个指定的表空间中为其分配存储空间.最初创建的表时一个空的逻辑存储结构,其中不包含任何数据 ...

  3. https小结

    目录: 1.什么是https 2.https实现过程描述(https和证书小结) 3.(在客户端)https抓包解密 4.wireshark分析https数据包解密前后的特点 正文 1.什么是http ...

  4. MySQL Backup--Xtrabackup备份限速问题

    在innobackupex 2.4版本中,有两个参数用来限制备份速度: --throttle=# This option specifies a number of I/O operations (p ...

  5. Android笔记(六十五) android中的动画——属性动画(propertyanimation)

    补间动画只能定义起始和结束两个帧在“透明度”.“旋转”.“倾斜”.“位移”4个方面的变化,逐帧动画也只能是播放多个图片,无法满足我们日常复杂的动画需求,所以谷歌在3.0开始,推出了属性动画(prope ...

  6. Linux命令——getconf

    转自:灵活使用getconf命令来获取系统信息 简介 getconf本身是个ELF可执行文件,用于获取系统信息 用法 getconf -a可以获取全部系统信息 对于这个命令,记住几个常用的信息获取方法 ...

  7. mysqldump 备份与恢复数据库

    备份数据库 mysqldump -u root -plvtao test > /home/bak.sql 数据库还原,常用source 命令登陆 mysql -u root -p mysql&g ...

  8. 2013.4.29 - KDD第十一天

    今天上午在图书馆写FIrst集,真心没写出来,算法是昨天找好的,不过实现的话还是需要很大的代码量,然后就打算用郑茂或者韩冰的代码了. 晚上图书馆快关门的时候开始思考KDD的问题, 我一开始打算给中秋发 ...

  9. cobbler相关

    Cobbler通过将设置和管理一个安装服务器所涉及的任务集中在一起,从而简化了系统配置.相当于Cobbler封装了DHCP.TFTP.XINTED等服务,结合了PXE.kickstart等安装方法,可 ...

  10. (一)WCF基础

    我们近期在做项目的时候用到了WCF,之前已经看了部分视频,对于WCF有了一定的了解,但仅限于能够根据搭建好的框架使用WCF,还不了解.所以就进行了研究,这样既有实践也能增加理论,二者结合,使用起来更胜 ...