一、效果图

二、代码

miniSearch.py

  1. from tkinter import *
  2. from tkinter import ttk, messagebox, filedialog
  3. from threading import Thread
  4. import os
  5. import queue
  6. import loadingDialog
  7. import re
  8.  
  9. class Application_UI(object):
  10. # 默认查找路径
  11. search_path = os.path.abspath("./")
  12. # 是否开始查找标志
  13. is_start = 0
  14.  
  15. def __init__(self):
  16. # 设置UI界面
  17. self.window = Tk()
  18. win_width = 600
  19. win_height = 500
  20. screen_width = self.window.winfo_screenwidth()
  21. screen_height = self.window.winfo_screenheight()
  22. x = int((screen_width - win_width) / 2)
  23. y = int((screen_height - win_height) / 2)
  24. self.window.title("磁盘文件搜索工具")
  25. self.window.geometry("%sx%s+%s+%s" % (win_width, win_height, x, y))
  26. # 最好用绝对路径
  27. self.window.iconbitmap(r"G:\PyCharm 2019.1\project\TK\项目\磁盘搜索工具\icon.ico")
  28. #
  29.  
  30. top_frame = Frame(self.window)
  31. top_frame.pack(side = TOP, padx = 5, pady = 20, fill = X)
  32.  
  33. self.search_val = StringVar()
  34. entry = Entry(top_frame, textvariable = self.search_val)
  35. entry.pack(side = LEFT, expand = True, fill = X, ipady = 4.5)
  36.  
  37. search_btn = Button(top_frame, text = "搜索", width = 10, command = self.search_file)
  38. search_btn.pack(padx = 10)
  39.  
  40. bottom_frame = Frame(self.window)
  41.  
  42. bottom_frame.pack(side = LEFT, expand = True, fill = BOTH, padx = 5)
  43.  
  44. tree = ttk.Treeview(bottom_frame, show = "headings", columns = ("name", "path"))
  45. self.treeView = tree
  46. y_scroll = Scrollbar(bottom_frame)
  47. y_scroll.config(command = tree.yview)
  48. y_scroll.pack(side = RIGHT, fill = Y)
  49. x_scroll = Scrollbar(bottom_frame)
  50. x_scroll.config(command = tree.yview, orient = HORIZONTAL)
  51. x_scroll.pack(side = BOTTOM, fill = X)
  52. tree.config(xscrollcommand = x_scroll.set, yscrollcommand = y_scroll.set)
  53.  
  54. tree.column("name", anchor = "w", width = 8)
  55. tree.column("path", anchor = "w")
  56. tree.heading("name", text = "文件名称", anchor = "w")
  57. tree.heading("path", text = "路径", anchor = "w")
  58. tree.pack(side = LEFT, fill = BOTH, expand = True, ipady = 20)
  59.  
  60. menu = Menu(self.window)
  61. self.window.config(menu = menu)
  62.  
  63. set_path = Menu(menu, tearoff = 0)
  64. set_path.add_command(label = "设置路径", accelerator="Ctrl + F", command = self.open_dir)
  65. set_path.add_command(label = "开始扫描", accelerator="Ctrl + T", command = self.search_file)
  66.  
  67. menu.add_cascade(label = "文件", menu = set_path)
  68.  
  69. about = Menu(menu, tearoff = 0)
  70. about.add_command(label = "版本", accelerator = "v1.0.0")
  71. about.add_command(label = "作者", accelerator = "样子")
  72. menu.add_cascade(label = "关于", menu = about)
  73.  
  74. self.progressbar = loadingDialog.progressbar()
  75. # 设置队列,保存查找完毕标志
  76. self.queue = queue.Queue()
  77. # 开始监听进度条
  78. self.listen_progressBar()
  79.  
  80. self.window.bind("<Control-Key-f>", lambda event: self.open_dir())
  81. self.window.bind("<Control-Key-r>", lambda event: self.search_file())
  82. self.window.protocol("WM_DELETE_WINDOW", self.call_close_window)
  83. self.window.mainloop()
  84.  
  85. class Application(Application_UI):
  86. def __init__(self):
  87. Application_UI.__init__(self)
  88.  
  89. def call_close_window(self):
  90. self.progressbar.exit_()
  91. self.window.destroy()
  92.  
  93. ''' 监听进度条'''
  94. def listen_progressBar(self):
  95. # 窗口每隔一段时间执行一个函数
  96. self.window.after(400, self.listen_progressBar)
  97. while not self.queue.empty():
  98. queue_data = self.queue.get()
  99. if queue_data == 1:
  100. # 关闭进度条
  101. self.progressbar.exit_()
  102. self.is_start = 0
  103.  
  104. ''' 设置默认搜索路径'''
  105. def open_dir(self):
  106. path = filedialog.askdirectory(title = u"设置目录", initialdir = self.search_path)
  107. print("设置路径:"+path)
  108. self.search_path = path
  109.  
  110. ''' 开始搜索'''
  111. def search_file(self):
  112. def scan(self, keyword):
  113. # 清空表格数据
  114. for _ in map(self.treeView.delete, self.treeView.get_children()):
  115. pass
  116.  
  117. # 筛选文件
  118. self.find_file_insert(keyword)
  119.  
  120. # 设置查找完毕标志
  121. self.queue.put(1)
  122.  
  123. if self.is_start == 0:
  124. # 获取查找关键词
  125. keyword = str.strip(self.search_val.get())
  126. if not keyword:
  127. messagebox.showerror("提示", "请输入文件名称")
  128. self.search_val.set("")
  129. return
  130.  
  131. # 设置已经开始查找状态
  132. self.is_start = 1
  133. # 开启线程
  134. self.thread = Thread(target = scan, args = (self, keyword))
  135. self.thread.setDaemon(True)
  136. self.thread.start()
  137.  
  138. # 显示进度条
  139. self.progressbar.start()
  140. else:
  141. pass
  142.  
  143. ''' 查找设置目录下所有文件并插入表格'''
  144. def find_file_insert(self, keyword):
  145. try:
  146. for root, dirs, files in os.walk(self.search_path, topdown = True):
  147. for file in files:
  148. match_result = self.file_match(file, keyword)
  149. if match_result is True:
  150. file_path = os.path.join(root, file)
  151. # 插入数据到表格
  152. self.treeView.insert('', END, values = (file, file_path))
  153. # 更新表格
  154. self.treeView.update()
  155. except Exception as e:
  156. print(e)
  157.  
  158. return True
  159.  
  160. ''' 名称匹配'''
  161. def file_match(self, file, keyword):
  162. print("文件匹配:",keyword, file)
  163. result = re.search(r'(.*)'+keyword+'(.*)', file, re.I)
  164. if result:
  165. return True
  166. return False
  167.  
  168. if __name__ == "__main__":
  169. 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. Web应用和Web框架

    一.Web应用 二.Web框架 三.wsgiref模块 一.Web应用 1.什么是Web应用? Web应用程序是一种可以通过Web访问的应用程序,特点是用户很容易访问,只需要有浏览器即可,不需要安装其 ...

  2. Markdown 初学总结

    Markdown Tutorial(Typora-Specific) 1. Headers 最多可有六级标题,在标题前加 # 作为标记.注意标记与标题内容之间有空格: # 这是一级标题 ## 这是二级 ...

  3. 【雅思】【绿宝书错词本】List25~36

    List 25 ❤arable a.可耕作的 n.耕地 ❤congested a.拥挤不堪的:充塞的 ❤split v.(使)分裂,分离:(被)撕裂:裂开:劈开:分担,分享n.裂口:分化 ,分裂 ❤n ...

  4. MySQL Case--应用服务器性能瓶颈导致慢SQL

    在分析优化慢SQL时,除考虑慢SQL对应执行计划外,还需要考虑 1. 慢SQL发生时间点的数据库服务器性能 2.慢SQL发生时间点的应用程序服务器性能 3. 慢SQL发生时间点数据库服务器和应用服务器 ...

  5. 在Linux主机使用命令行批量删除harbor镜像

     在Linux主机使用命令行批量删除harbor镜像 脚本使用说明: 此脚本不是万能脚本,根据自身环境要调整很多 能用harbor的域名就不要用IP 脚本前半部分可以套用,后半部分需一步一步试错,结合 ...

  6. c# 写入文本文件

  7. pom中添加插件打包上传源码

    <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> ...

  8. JSONObject对象

    1.JSONObject介绍 JSONObject-lib包是一个beans,collections,maps,java arrays和xml和JSON互相转换的包. 方法: 的getString() ...

  9. typescript 模块

    模块:模块可以帮助开发者将代码分割为重用的单元.开发者可以自己决定将模块中的哪些资源(类,方法,变量)暴露出去供外部使用,哪些资源只在模块内使用 在ts里面,一个文件就是一个模块,并没有什么特殊的标识 ...

  10. IDEA实用教程(十)—— 配置Maven的全局设置

    使用之前需要提前安装好Maven 第一步 第二步