python初学者案例,桌面文件搜索小程序的实现

import os
import tkinter as tk
import glob
from tkinter.filedialog import *
# import threading
import time

def openFile():
    dirname = askdirectory()
    # dn = tk.StringVar()
    # dn.set(dirname)
    entry.delete(0, tk.END)
    entry.insert(0, dirname)

    print(dirname)

# def start():
#     threading.Thread(target=startSearch, args='').start()

def startSearch():
    print('开始搜索…………')
    v_value = v.get()
    if v_value == 1:
        value_content = 0  # 保存单选钮设置的文件大小
    elif v_value == 2:
        value_content = 500
    elif v_value == 3:
        value_content = 1000
    begin = time.time()
    file_path = entry.get()
    os.chdir(r'%s' % file_path.split()[0])
    files = glob.glob('**/%s' % file_path.split()[1], recursive = True)
    lis.delete(0, tk.END)

    try:
        lab_search.config(text='搜索开始....', fg='red', font=('仿宋', 15))
        for line in files:
            if os.stat(line).st_size/1024/1024 >= value_content:
                lis.insert(0, line)
                print(line,os.stat(line).st_size/1024/1024)
    except:
        print('filename error found!')
    lab_search.config(text = '搜索到文件{0:*^10d}个'.format(len(files)), fg = 'blue', font = ('仿宋', 10))
    btn_quit.grid(row=3, column=2, sticky=tk.S)
    end = time.time()
    print('本程序共计用时{0:*^25s}秒'.format(str(end - begin)))
    print('本程序共计找到文件{0:*^25d}个'.format(len(files)))


root = tk.Tk()
root.geometry('500x590+800+10')
root.title('文件快速检索小应用')
label = tk.Label(root, text = '请输入搜索路径:', font = ('仿宋', 15))
label.grid(row = 0, column = 0)
pp = tk.PhotoImage(file = r"D:\办公自动化\openfile.png")
btn = tk.Button(root,image = pp, width = 20, height = 20, command = openFile)
btn.grid(row = 0, column = 1)
dname = 'd:\\ *.py'

entry = tk.Entry(root, font= ('仿宋', 15), width = 25,textvariable = dname)
entry.insert(0, dname)

entry.grid(row = 0, column = 2, sticky = tk.E)
frm = tk.Frame(root, relief=tk.GROOVE, borderwidth=3,padx = 63, pady = 0)
v = IntVar()
v.set(1)
rb1 = tk.Radiobutton(frm, text='不分大小', font= ('仿宋', 15),variable=v, value=1)
rb1.grid(row = 1, column = 0, sticky = tk.W)

rb2 = tk.Radiobutton(frm, text='大于500MB', font= ('仿宋', 15),variable=v, value=2)
rb2.grid(row = 1, column = 1, sticky = tk.W)

rb3 = tk.Radiobutton(frm, text='大于1000MB', font= ('仿宋', 15),variable=v, value=3)
rb3.grid(row = 1, column = 2, sticky = tk.W)
frm.grid(row = 1, columnspan = 3 )

lis = tk.Listbox(root, font = ('仿宋', 15),width = 49, height = 23)
lis.grid(row = 2, columnspan = 3,sticky = tk.W)
btn_start = tk.Button(text = '开始搜索',font = ('仿宋', 15), command = startSearch)
btn_start.grid(row = 3, column = 0, sticky = tk.W)
lab_search = tk.Label(text = '未开始', font = ('仿宋', 15), fg = 'red')
lab_search.grid(row = 3, column = 1, sticky = tk.S)
btn_quit = tk.Button(text = '结束运行',font = ('仿宋', 15), command = root.quit)
btn_quit.grid(row = 3, column = 2, sticky = tk.E)


root.mainloop()

 

 

posted @ 2020-05-19 11:58  Iceberg_710815  阅读(537)  评论(0编辑  收藏  举报