pyhthon桌面搜索工具

代码实现

说明:采用thinker库来实现对文件的类型和文件的名字进行操作

"""

    做一个桌面应用搜索程序

"""
# 导入tkinter库,as将这个库取简化别名为tk
import tkinter as tk
from tkinter import messagebox, filedialog
import os

root = tk.Tk()
root.title('搜索工具')
root.geometry('600x300')

# 布局组件
searchFrame = tk.Frame()
searchFrame.pack()

# 关键字标签
tk.Label(searchFrame, text='关键字:').pack(side=tk.LEFT, padx=10, pady=10)
searchKey = tk.Entry(searchFrame)
searchKey.pack(side=tk.LEFT, padx=10, pady=10)

# 文件类型标签
tk.Label(searchFrame, text='文件类型:').pack(side=tk.LEFT, padx=10, pady=10)
searchType = tk.Entry(searchFrame)
searchType.pack(side=tk.LEFT, padx=10, pady=10)

# 搜索标签
searchButton = tk.Button(searchFrame,text='搜索')
searchButton.pack(side=tk.LEFT, padx=10, pady=10)

# 列表框
searchListBox = tk.Listbox(root, width=80)
searchListBox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

# 滚动栏
searchScrollBar = tk.Scrollbar(root)
searchScrollBar.pack(side=tk.RIGHT, fill=tk.Y)

def search():
    fileCounts = []
    key = searchKey.get()
    fileType = searchType.get()
    if not key:
        messagebox.showinfo(title='出错了!', message='请输入正确的关键字!')
        return
    if not fileType:
        messagebox.showinfo(title='出错了!', message='请输入正确的文件类型!')
        return

    if key and fileType:
        # 开始执行程序代码
        print('开始执行搜索!')
        # 打印搜索的关键字和文件类型
        print('关键字:', key)
        print('文件类型:', fileType)
        # 调用 Windows 文件管理系统,获取指定的目录
        fn = filedialog.askdirectory()
        searchListBox.delete(0, tk.END)
        # 开始遍历选取的文件夹
        # rootPath 表示根目录/目录列表/..文件
        for rootPath, dirs, files in os.walk(fn):
            # print(rootPath,dirs,files)
            # 遍历文件
            for file in files: # 循环遍历列表文件
                if file.endswith(fileType):# 对比文件类型使用 endswith 函数
                    # 目前条件是对比key值与file(即对照文件名是否与搜索条件相一致)的值,
                    # 若把if条件 ,即 70行代码注释,则可以实现按照文件的类型来进行条件的筛选
                    if (key in file) and (file.endswith(fileType)):
                        fileCounts.append(file)
                        searchListBox.insert(tk.END, rootPath + '\\' + file)
    # 将搜索的记录进行统计,并且输出到ListBox中显示:
    searchListBox.insert(tk.END, '总共搜索到:' + str(len(fileCounts)) + '条!')
# 将按钮键与搜索功能绑定
searchButton.config(command=search)
# 设置滚动条样式
searchListBox.config(yscrollcommand=searchScrollBar.set)
searchScrollBar.config(command=searchListBox.yview)

def resultsDisplay (event) :
    print('条目被点击了!')
    # 获取当前被选中的条目
    selected = searchListBox.curselection()[0] # 获取键值
    # 获取选中文件的路径
    selectedFilePath = searchListBox.get(selected)
    # 获取文件内容
    content = open(selectedFilePath, mode='r', encoding='utf-8').read()
    # 测试用例可以注释掉
    # content1 = open(selectedFilePath, mode='r', encoding='ISO-8859-1').read()
    # content = content1.encode('iso-8859-1').decode('gbk').encode('utf8').decode('utf8')
    # 新建top窗口
    top = tk.Toplevel()
    top.title('查看内容')
    # top.geometry('300x300')

    # 新建标签
    text = tk.Text(top) # 布局到top窗口上
    text.pack(side=tk.LEFT, expand=True)
    # 将获取到的内容插入到text窗口
    text.insert(tk.END, content)

    # 滚动条
    sb = tk.Scrollbar(top) # 布局到top窗口
    sb.pack(side=tk.RIGHT, fill=tk.Y)
    # 设置滚动条的方式
    sb.config(command=text.yview)
    text.config(yscrollcommand=sb.set)


searchListBox.bind('<Double-Button-1>', resultsDisplay)
tk.mainloop()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值