Python开发电脑文件搜索工具, 让你硬盘中的秘密文件文件无处可藏(附完整源码)

19 篇文章 4 订阅

前言

嗨喽,大家好吖~这里是魔王

你是不是现在在用python做爬虫,那么其实我们爬取的网站,也可以用python去开发

今天带你初步了解 python的一个模块 tkinter,并且带领大家完成一个简单的电脑文件搜索工具

让你硬盘中的秘密文件文件无处可藏

请添加图片描述

课题:

Python开发电脑文件搜索工具

在这里插入图片描述

课程收获:

  • tkinter 的使用
  • 桌面应用程序开发

开发环境:

  1. 解释器: Python 3.6.5 | Anaconda, Inc.
  2. 编辑器: pycharm 专业版

请添加图片描述

导入模块

import tkinter as tk
from tkinter import messagebox, filedialog
import os

在这里插入图片描述

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

"""搜索内容 从左到右布局"""
search_frame = tk.Frame()
search_frame.pack()

padding 边框

tk.Label(search_frame, text='关键字:').pack(side=tk.LEFT, padx=10, pady=10)
key_entry = tk.Entry(search_frame)
key_entry.pack(side=tk.LEFT, padx=10, pady=10)

tk.Label(search_frame, text='文件类型:').pack(side=tk.LEFT, padx=10, pady=10)
type_entry = tk.Entry(search_frame)
type_entry.pack(side=tk.LEFT, padx=10, pady=10)

按钮

search_button = tk.Button(search_frame, text='搜索')
search_button.pack(side=tk.LEFT, padx=10, pady=10)

把搜索的内容文件插入到列表盒子李阿敏

list_box = tk.Listbox(root, width=80)
list_box.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)


def search():
    print('按钮被点击了')
    # 点击按钮获取输入的数据
    key = key_entry.get()
    file_type = type_entry.get()
    print('搜索关键字:', key)
    print('文件类型:', file_type)
    # 没输入内容的时候弹窗显示错误
    if not key:
        messagebox.showinfo(title='出错了', message='请输入关键字')
        return
    if not file_type:
        messagebox.showinfo(title='出错了', message='请输入文件类型')
        return
    # 获取文件窗口搜索文件的目录
    fn = filedialog.askdirectory()
    print(fn)
    # 变量目录下的所有文件
    fn_list = os.walk(fn)
    for root_path, dirs, files in fn_list:
        # root_path 根目录 中间目录 所有的文件列表
        # 根目录 + 文件列表里面的文件
        # print(root_path, dirs, files)
        # 只要获取所有的文件就可以了
        for file in files:
            if file.endswith(file_type):
                print(root_path + '\\' + file)
                # 只需要指定的文件类型
                list_box.insert(tk.END, root_path + '\\' + file)

请添加图片描述

因为规定不能加 函数调用与函数对象

search_button.config(command=search)

然后调用文件管理器搜索文件

滚动栏

sb = tk.Scrollbar(root)
sb.pack(side=tk.RIGHT, fill=tk.Y)

设置 Text 文本框

list_box.config(yscrollcommand=sb.set)
sb.config(command=list_box.yview)


def list_box_click(event):
    print('列表盒子被点击了')
    # 获取列表盒子被选中的内容
    index = list_box.curselection()[0]
    file_path = list_box.get(index)
    print(file_path)
    # 得到路径之后,然后将内容显示到新的窗口
    top = tk.Toplevel()
    top.title("查看内容")
    content = open(file_path, mode='r', encoding='utf-8').read()
    text = tk.Text(top)
    text.pack(side=tk.LEFT)
    text.insert(tk.END, content)
    # 滚动栏
    sb = tk.Scrollbar(top)
    sb.pack(side=tk.RIGHT, fill=tk.Y)
    # 设置 Text 文本框
    text.config(yscrollcommand=sb.set)
    sb.config(command=text.yview)

当列表盒子被点击的时候,弹出新的窗口

list_box.bind('<Double-Button-1>', list_box_click)
root.mainloop()

在这里插入图片描述
好了,我的这篇文章写到这里就结束啦!

有更多建议或问题可以评论区或私信我哦!一起加油努力叭(ง •_•)ง

喜欢就关注一下博主,或点赞收藏评论一下我的文章叭!!!

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值