亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

python仿evething的文件搜索器實例代碼

系統 1960 0

今天看到everything搜索速度秒殺windows自帶的文件管理器,所以特地模仿everything實現了文件搜索以及打開對應文件的功能,首先來一張搜索對比圖。

這是evething搜索效果:

python仿evething的文件搜索器實例代碼_第1張圖片

這是自己實現的效果:

python仿evething的文件搜索器實例代碼_第2張圖片

主要功能就是python的os庫的文件列表功能,sqllite創建表,插入數據以及模糊搜索,然后就是tkiner實現的界面功能。全部代碼貼出來做一次記錄,花費一天時間踩坑。

            
# coding=utf-8
import tkinter as tk
import tkinter.messagebox #這個是消息框,對話框的關鍵
import tkinter.constants
import sqlite3
import os
import threading
import traceback
 
def update_db():
  print("更新數據庫")
  tkinter.messagebox.showerror("錯誤提示","更新數據庫功能未完待續,可以刪除目錄下的allFiles.db文件,然后點擊搜索,即可刷新數據庫")
 
def mouseCallBack(*args):
  indexs = listb.curselection()
  index = int(indexs[0])
  print("index",index)
  start_directory = str(myArr[index])
  print(start_directory[2:-3])
  os.startfile(start_directory[2:-3])
 
def obtain_all_files(filepath,cursor):
#遍歷filepath下所有文件,包括子目錄
 try:
   files = os.listdir(filepath)
   for fi in files:
    fi_d = os.path.join(filepath,fi)
    if os.path.isdir(fi_d):
     obtain_all_files(fi_d,cursor)
    else:
     path = os.path.join(filepath,fi_d)
     update_progress.set(path)
     print("目錄",path)
     sqlAdd = "insert into filepath (file_path) values ('"+path+"')"
     print("sqlAdd",sqlAdd)
     cursor.execute(sqlAdd)
 except Exception as e:
   traceback.print_exc()
   print("掃描文件出異常了,點擊確定繼續掃描")
   tkinter.messagebox.showerror("錯誤提示","掃描文件出異常了看,點擊確定繼續掃描")
 
 
 
 
def scan_file():
  print("開始掃描文件")
 #  del myArr[:]
  connection.execute("BEGIN TRANSACTION;") # 關鍵點
  cursor = connection.cursor()
  obtain_all_files('G:\\',cursor)
  print("G盤掃描完成...")
  tkinter.messagebox.showinfo("溫馨提示","G盤掃描完成....")
  connection.execute("COMMIT;") #關鍵點
  connection.commit()
  connection.close()
 
 
def insert_db():
   t1 = threading.Thread(target=scan_file)
   t1.setDaemon(True)
   t1.start()
   tkinter.messagebox.showinfo("溫馨提示","正在更新數據庫,請等待...")
 
def search_file():
   #表示創建一個數據庫,并獲得連接
   print("數據庫是否存在: ",isExistDB)
   if(isExistDB==False):
     tkinter.messagebox.showwarning("警告","數據庫不存在,將更新數據庫文件!")
     try:
       mycursor = connection.cursor()
       file_sql = "create table filepath('file_path' text not null)"
       mycursor.execute(file_sql)
       mycursor.close()
       insert_db()
     except:
       tkinter.messagebox.showerror("錯誤提示","數據庫發生異常...")
       return
   else:
     print("開始搜索")
     listb.delete(0,tk.constants.END)
     mycursor = connection.cursor()
     entry_text = inputText.get()
     search_sql = "select * from filepath where file_path like '%"+entry_text+"%'"
     files = mycursor.execute(search_sql)
     #tkinter.messagebox.showwarning("警告","沒有找到對應的文件!")
     for f in files:
      print(f)
      myArr.append(f)
      listb.insert(tkinter.constants.END,f)
     print("搜索完成")
     mycursor.close()
 
myArr = []
isExistDB = os.path.exists("allFiles.db")
connection = sqlite3.connect("allFiles.db",check_same_thread = False)
root = tk.Tk() # 初始化Tk()
root.title("電腦文件搜索工具(仿everything)By景兄弟V1.0")  # 設置窗口標題
root.geometry("800x600")  # 設置窗口大小 注意:是x 不是*
root.resizable(width=False, height=False) # 設置窗口是否可以變化長/寬,False不可變,True可變,默認為True
#設置輸入框
inputText = tk.Entry(root,show=None,foreground = 'red',font = ('Helvetica', '15', 'bold'),insertbackground = 'green',width=65)
inputText.pack()
#設置按鈕,以及放置的位置
searchBtn = tk.Button(root, text="搜索", fg="blue",bd=2,width=10,command=search_file)#command中的方法帶括號是直接執行,不帶括號才是點擊執行
searchBtn.place(x=200, y=40, anchor='nw')
updateBtn = tk.Button(root, text="更新數據庫", fg="blue",bd=2,width=10,command=update_db)
updateBtn.place(x=400, y=40, anchor='nw')
 
update_progress = tk.StringVar()
update_progress.set('還未開始掃描')
lb = tk.Label(root,text="還未開始", fg="blue",bd=2,width=100, textvariable=update_progress)
lb.place(x=20,y=90)
 
listb = tk.Listbox(root,width=110,height=20)
listb.place(x=1, y=120, anchor='nw')
sb = tk.Scrollbar(root)  #垂直滾動條組件
sb.pack(side=tkinter.constants.RIGHT,fill=tkinter.constants.Y) #設置垂直滾動條顯示的位置
listb.config(yscrollcommand=sb.set)
listb.bind("<
            
              >",mouseCallBack)
root.mainloop() # 進入消息循環

            
          

以上所述是小編給大家介紹的python仿evething的文件搜索器詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 66精品综合久久久久久久 | 这里只有久久精品 | 69成人网| 美女福利在线观看 | 精品久久视频 | 国产高清在线精品一区二区 | 在线观看中文字幕一区 | 亚洲成人在线视频 | 2021福利视频 | 麻豆伦理 | 国产精品1区2区3区在线播放 | 亚洲不卡在线观看 | 久久这里只有精品66 | 亚洲九色 | 国产成人精品久久综合 | 在线观看日韩精品 | 亚洲国产高清一区二区三区 | 亚洲精品久久中文字幕 | 天天摸天天碰色综合网 | 国产精品视频网 | 国产成人精品本亚洲 | 亚洲欧美另类在线视频 | 99视频精品全部国产盗摄视频 | 国产毛茸茸| 四虎视频国产在线观看 | 想看一级毛片 | 精品国产调教最大网站女王 | 欧美日韩精品国产一区二区 | 波多野野结衣1区二区 | 欧美不卡在线 | 四虎新地址4hu 你懂的 | 久久精品亚洲欧美日韩久久 | 色综合久久一区二区三区 | 啊用力嗯快国产在线观看 | 亚洲精品成人一区二区aⅴ 亚洲精品成人一区二区www | 狠狠色噜噜狠狠狠狠色综合久 | 欧美aaaaa一级毛片在线 | 国产一区成人 | 四虎欧美在线观看免费 | 日韩视频一区二区在线观看 | 一级毛毛片 |