python 遍历listbox_Python仿evething的文件搜索器 !
今天看到everything搜索速度秒殺windows自帶的文件管理器,所以特地模仿everything實(shí)現(xiàn)了文件搜索以及打開對應(yīng)文件的功能,首先來一張搜索對比圖。
這是evething搜索效果:
Python學(xué)習(xí)交流群:1004391443,這里有資源共享,技術(shù)解答,還有小編從最基礎(chǔ)的Python資料到項(xiàng)目實(shí)戰(zhàn)的學(xué)習(xí)資料都有整理,希望能幫助你更了解python,學(xué)習(xí)python。
這是自己實(shí)現(xiàn)的效果:
主要功能就是python的os庫的文件列表功能,sqllite創(chuàng)建表,插入數(shù)據(jù)以及模糊搜索,然后就是tkiner實(shí)現(xiàn)的界面功能。全部代碼貼出來做一次記錄,花費(fèi)一天時間踩坑。
# coding=utf-8import tkinter as tkimport tkinter.messagebox #這個是消息框,對話框的關(guān)鍵import tkinter.constantsimport sqlite3import osimport threadingimport traceback def update_db(): print("更新數(shù)據(jù)庫") tkinter.messagebox.showerror("錯誤提示","更新數(shù)據(jù)庫功能未完待續(xù),可以刪除目錄下的allFiles.db文件,然后點(diǎn)擊搜索,即可刷新數(shù)據(jù)庫") 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("掃描文件出異常了,點(diǎn)擊確定繼續(xù)掃描") tkinter.messagebox.showerror("錯誤提示","掃描文件出異常了看,點(diǎn)擊確定繼續(xù)掃描") def scan_file(): print("開始掃描文件") # del myArr[:] connection.execute("BEGIN TRANSACTION;") # 關(guān)鍵點(diǎn) cursor = connection.cursor() obtain_all_files('G:',cursor) print("G盤掃描完成...") tkinter.messagebox.showinfo("溫馨提示","G盤掃描完成....") connection.execute("COMMIT;") #關(guān)鍵點(diǎn) connection.commit() connection.close() def insert_db(): t1 = threading.Thread(target=scan_file) t1.setDaemon(True) t1.start() tkinter.messagebox.showinfo("溫馨提示","正在更新數(shù)據(jù)庫,請等待...") def search_file(): #表示創(chuàng)建一個數(shù)據(jù)庫,并獲得連接 print("數(shù)據(jù)庫是否存在: ",isExistDB) if(isExistDB==False): tkinter.messagebox.showwarning("警告","數(shù)據(jù)庫不存在,將更新數(shù)據(jù)庫文件!") 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("錯誤提示","數(shù)據(jù)庫發(fā)生異常...") 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("警告","沒有找到對應(yīng)的文件!") 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") # 設(shè)置窗口標(biāo)題root.geometry("800x600") # 設(shè)置窗口大小 注意:是x 不是*root.resizable(width=False, height=False) # 設(shè)置窗口是否可以變化長/寬,False不可變,True可變,默認(rèn)為True#設(shè)置輸入框inputText = tk.Entry(root,show=None,foreground = 'red',font = ('Helvetica', '15', 'bold'),insertbackground = 'green',width=65)inputText.pack()#設(shè)置按鈕,以及放置的位置searchBtn = tk.Button(root, text="搜索", fg="blue",bd=2,width=10,command=search_file)#command中的方法帶括號是直接執(zhí)行,不帶括號才是點(diǎn)擊執(zhí)行searchBtn.place(x=200, y=40, anchor='nw')updateBtn = tk.Button(root, text="更新數(shù)據(jù)庫", 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) #設(shè)置垂直滾動條顯示的位置listb.config(yscrollcommand=sb.set)listb.bind("<<ListboxSelect>>",mouseCallBack)root.mainloop() # 進(jìn)入消息循環(huán)今天看到everything搜索速度秒殺windows自帶的文件管理器,所以特地模仿everything實(shí)現(xiàn)了文件搜索以及打開對應(yīng)文件的功能,首先來一張搜索對比圖。
這是evething搜索效果:
這是自己實(shí)現(xiàn)的效果:
主要功能就是python的os庫的文件列表功能,sqllite創(chuàng)建表,插入數(shù)據(jù)以及模糊搜索,然后就是tkiner實(shí)現(xiàn)的界面功能。全部代碼貼出來做一次記錄,花費(fèi)一天時間踩坑。
# coding=utf-8import tkinter as tkimport tkinter.messagebox #這個是消息框,對話框的關(guān)鍵import tkinter.constantsimport sqlite3import osimport threadingimport traceback def update_db(): print("更新數(shù)據(jù)庫") tkinter.messagebox.showerror("錯誤提示","更新數(shù)據(jù)庫功能未完待續(xù),可以刪除目錄下的allFiles.db文件,然后點(diǎn)擊搜索,即可刷新數(shù)據(jù)庫") 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("掃描文件出異常了,點(diǎn)擊確定繼續(xù)掃描") tkinter.messagebox.showerror("錯誤提示","掃描文件出異常了看,點(diǎn)擊確定繼續(xù)掃描") def scan_file(): print("開始掃描文件") # del myArr[:] connection.execute("BEGIN TRANSACTION;") # 關(guān)鍵點(diǎn) cursor = connection.cursor() obtain_all_files('G:',cursor) print("G盤掃描完成...") tkinter.messagebox.showinfo("溫馨提示","G盤掃描完成....") connection.execute("COMMIT;") #關(guān)鍵點(diǎn) connection.commit() connection.close() def insert_db(): t1 = threading.Thread(target=scan_file) t1.setDaemon(True) t1.start() tkinter.messagebox.showinfo("溫馨提示","正在更新數(shù)據(jù)庫,請等待...") def search_file(): #表示創(chuàng)建一個數(shù)據(jù)庫,并獲得連接 print("數(shù)據(jù)庫是否存在: ",isExistDB) if(isExistDB==False): tkinter.messagebox.showwarning("警告","數(shù)據(jù)庫不存在,將更新數(shù)據(jù)庫文件!") 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("錯誤提示","數(shù)據(jù)庫發(fā)生異常...") 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("警告","沒有找到對應(yīng)的文件!") 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") # 設(shè)置窗口標(biāo)題root.geometry("800x600") # 設(shè)置窗口大小 注意:是x 不是*root.resizable(width=False, height=False) # 設(shè)置窗口是否可以變化長/寬,False不可變,True可變,默認(rèn)為True#設(shè)置輸入框inputText = tk.Entry(root,show=None,foreground = 'red',font = ('Helvetica', '15', 'bold'),insertbackground = 'green',width=65)inputText.pack()#設(shè)置按鈕,以及放置的位置searchBtn = tk.Button(root, text="搜索", fg="blue",bd=2,width=10,command=search_file)#command中的方法帶括號是直接執(zhí)行,不帶括號才是點(diǎn)擊執(zhí)行searchBtn.place(x=200, y=40, anchor='nw')updateBtn = tk.Button(root, text="更新數(shù)據(jù)庫", 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) #設(shè)置垂直滾動條顯示的位置listb.config(yscrollcommand=sb.set)listb.bind("<<ListboxSelect>>",mouseCallBack)root.mainloop() # 進(jìn)入消息循環(huán)總結(jié)
以上是生活随笔為你收集整理的python 遍历listbox_Python仿evething的文件搜索器 !的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: threejs模型可视化编辑器_一个近乎
- 下一篇: 互动整合营销_营销成本日益升高,整合营销
