• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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()

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
uniapp 小程序echarts 使用发布时间:2022-07-18
下一篇:
小程序图片剪裁发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap