from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from Search_Apps import Find_APP
import threading
import pyperclip
'''
-treeview顯示搜索結(jié)果
-Menu綁定復(fù)制鏈接
'''
class App:
def __init__(self):
self.w=Tk()
self.w.title('應(yīng)用搜索工具(本地版)-v1.0')
width=590
height=395
left=(self.w.winfo_screenwidth()-width)/2
top=(self.w.winfo_screenheight()-height)/2
self.w.resizable(0,0)
self.w.geometry('%dx%d+%d+%d'%(width,height,left,top))
self.create_widet()
self.set_widget()
self.place_widget()
self.w.mainloop()
def create_widet(self):
self.l2_var=StringVar()
self.l1=ttk.Label(self.w,text='關(guān)鍵字:')
self.e1=ttk.Entry(self.w)
self.b1=ttk.Button(self.w,text='搜索')
self.tree=ttk.Treeview(self.w)
self.S_coll_vertical = Scrollbar(self.w, orient=VERTICAL)
self.l2=ttk.Label(self.w,textvariable=self.l2_var)
self.m=Menu(self.w)
self.w['menu']=self.m
self.m2=Menu(self.tree,tearoff=False)
def set_widget(self):
self.b1.config(command=lambda :self.thread_it(self.search_app))
self.e1.config(justify='center')
columns=('no','app_name','app_cate','size','app_intro')
self.tree.config(show='headings',columns=columns,selectmode=BROWSE,displaycolumns ='#all')
self.tree.column("no", anchor="center",minwidth=40,width=40, stretch=NO)
self.tree.column("app_name", anchor="center",minwidth=50,width=80, stretch=NO)
self.tree.column("app_cate", anchor="center",minwidth=50,width=80, stretch=NO)
self.tree.column("size", anchor="center",minwidth=50,width=80, stretch=NO)
self.tree.column("app_intro", anchor="center",minwidth=10,width=100)
self.tree.heading("no", text="序號")
self.tree.heading("app_name", text="名稱")
self.tree.heading("app_cate", text="類別")
self.tree.heading("size", text="大小")
self.tree.heading("app_intro", text="介紹")
self.tree.bind('TreeviewSelect>>',self.display_infos)
self.S_coll_vertical.config(command=self.tree.yview)
self.tree['yscrollcommand'] = self.S_coll_vertical.set
self.l2.config(background='lightblue',justify='center')
self.l2_var.set('請先搜索')
self.s1=Menu(self.m,tearoff=False)
self.s2=Menu(self.m,tearoff=False)
self.m.add_cascade(label='操作',menu=self.s1)
self.m.add_cascade(label='關(guān)于',menu=self.s2)
self.s1.add_command(label='搜索',command=lambda :self.thread_it(self.search_app))
self.s1.add_command(label='復(fù)制下載地址',command=lambda:self.thread_it(self.copy_apklink))
self.s1.add_separator()
self.s1.add_command(label='退出',command=self.quit_window)
self.s2.add_command(label='說明',command=self.show_explain)
self.s2.add_command(label='聯(lián)系作者',command=self.show_info)
self.w.protocol('WM_DELETE_WINDOW',self.quit_window)
self.m2.add_command(label='復(fù)制鏈接',command=self.copy_apklink)
self.tree.bind('Button-3>',self.copy_link)
def place_widget(self):
self.l1.place(x=70,y=20)
self.e1.place(x=150,y=20,width=250)
self.b1.place(x=430,y=18)
self.tree.place(x=10,y=60,width=570,height=300)
self.S_coll_vertical.place(x=570,y=60,height=300)
self.l2.place(x=10,y=367,width=570)
def search_app(self):
#清空treeview數(shù)據(jù)
for item in self.tree.get_children():
self.tree.delete(item)
key_word=self.e1.get()
if key_word:
self.l2_var.set(f'正在檢索......')
self.data=Find_APP().search_app(key_word)
if self.data:
i=0
for v in self.data:
self.tree.insert('',i,values=(i+1,v.get('app_name'),v.get('app_cate'),v.get('size'),v.get('app_intro')))
i+=1
self.l2.config(background='lightblue')
self.l2_var.set(f'一共檢索到[{len(self.data)}]個關(guān)于[{key_word}]的應(yīng)用')
elif self.data is False:
self.l2.config(background='red')
self.l2_var.set(f'數(shù)據(jù)庫連接失敗,請檢查數(shù)據(jù)庫配置!')
else:
self.l2.config(background='green')
self.l2_var.set(f'沒有檢索到關(guān)于[{key_word}]的應(yīng)用')
else:
messagebox.showwarning('警告','請輸入關(guān)鍵字!')
self.l2.config(background='red')
self.l2_var.set(f'請輸入關(guān)鍵字!')
def display_infos(self,event):
#獲取treeview當(dāng)前選中項數(shù)據(jù)
curr=self.tree.item(self.tree.focus()).get('values')
#獲取treeview當(dāng)前選中項索引
# curr_index = self.tree.index(self.tree.focus())
# app=self.data[curr_index]
self.l2_var.set(f'{curr[-1]}')
def show_info(self):
messagebox.showinfo('聯(lián)系作者', '作者QQ:xxxx')
def show_explain(self):
messagebox.showinfo('說明', '\r本軟件僅供學(xué)習(xí),請勿用于商業(yè)用途\n\n1.在輸入框輸入關(guān)鍵字進行搜索\n2.選擇應(yīng)用右擊提取下載地址')
def copy_link(self,event):
self.m2.post(event.x_root, event.y_root)
def copy_apklink(self):
try:
curr_index = self.tree.index(self.tree.focus())
app_link=self.data[curr_index].get('app_link')
pyperclip.copy(app_link)
messagebox.showinfo('提示','下載地址已成功復(fù)制到剪切板!')
except AttributeError:
messagebox.showwarning('警告','請先選中應(yīng)用!')
self.l2.config(background='red')
self.l2_var.set('請先選中應(yīng)用!')
def quit_window(self):
ret=messagebox.askyesno('退出','是否要退出?')
if ret:
self.w.destroy()
def thread_it(self,func,*args):
t=threading.Thread(target=func,args=args)
t.setDaemon(True)
t.start()
if __name__ == '__main__':
a=App()
本次使用tkinter制作了一款應(yīng)用搜索工具,使用其中的Treeview顯示搜索結(jié)果,使用Menu綁定相關(guān)操作,通過執(zhí)行Mongodb查詢語句得到相關(guān)數(shù)據(jù),所以要結(jié)合本地Mongodb數(shù)據(jù)庫,整體執(zhí)行效率高于網(wǎng)絡(luò)接口訪問,有機會的話,可能再發(fā)一篇通過接口得到數(shù)據(jù)的GUI,思路、代碼方面有什么不足歡迎各位大佬指正、批評!
以上就是python 制作本地應(yīng)用搜索工具的詳細(xì)內(nèi)容,更多關(guān)于python 制作應(yīng)用搜索工具的資料請關(guān)注腳本之家其它相關(guān)文章!