使用python怎么編寫一個(gè)本地應(yīng)用搜索工具-創(chuàng)新互聯(lián)

這篇文章主要介紹了使用python怎么編寫一個(gè)本地應(yīng)用搜索工具,此處通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考價(jià)值,需要的朋友可以參考下:

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供新興網(wǎng)站建設(shè)、新興做網(wǎng)站、新興網(wǎng)站設(shè)計(jì)、新興網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、新興企業(yè)網(wǎng)站模板建站服務(wù),10余年新興做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

python可以做什么

Python是一種編程語言,內(nèi)置了許多有效的工具,Python幾乎無所不能,該語言通俗易懂、容易入門、功能強(qiáng)大,在許多領(lǐng)域中都有廣泛的應(yīng)用,例如最熱門的大數(shù)據(jù)分析,人工智能,Web開發(fā)等。

設(shè)計(jì)思路

使用python怎么編寫一個(gè)本地應(yīng)用搜索工具

四.源代碼

GUI.py


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="序號(hào)")
  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('請(qǐng)先搜索')
  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)}]個(gè)關(guān)于[{key_word}]的應(yīng)用')
   elif self.data is False:
    self.l2.config(background='red')
    self.l2_var.set(f'數(shù)據(jù)庫連接失敗,請(qǐng)檢查數(shù)據(jù)庫配置!')
   else:
    self.l2.config(background='green')
    self.l2_var.set(f'沒有檢索到關(guān)于[{key_word}]的應(yīng)用')
  else:
   messagebox.showwarning('警告','請(qǐng)輸入關(guān)鍵字!')
   self.l2.config(background='red')
   self.l2_var.set(f'請(qǐng)輸入關(guān)鍵字!')

 def display_infos(self,event):
  #獲取treeview當(dāng)前選中項(xiàng)數(shù)據(jù)
  curr=self.tree.item(self.tree.focus()).get('values')
  #獲取treeview當(dāng)前選中項(xià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í),請(qǐng)勿用于商業(yè)用途\n\n1.在輸入框輸入關(guān)鍵字進(jì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('警告','請(qǐng)先選中應(yīng)用!')
   self.l2.config(background='red')
   self.l2_var.set('請(qǐng)先選中應(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()

 Search_Apps.py

import pymongo

class Find_APP(object):
 def __init__(self):
  self.Mongo_host='127.0.0.1'
  self.Mongo_port=27017

 def connect_db(self):
  try:
   conn=pymongo.MongoClient(host=self.Mongo_host,port=self.Mongo_port)
   self.db=conn.HuaWei
   self.myset=self.db.app_infos
   return True
  except:
   return False

 "{app_name: {$regex:/keyword/}}"#使用正則mongodb模糊查詢
 def search_app(self,key_word):
  if self.connect_db():
   app_data=[]
   sentence={'app_name': {"$regex":key_word}}
   try:
    for i in self.myset.find(sentence):
     i.pop('_id')
     app_data.append(i)
    return app_data
   except :
    return False
  else:
   return False

到此這篇關(guān)于使用python怎么編寫一個(gè)本地應(yīng)用搜索工具的文章就介紹到這了,更多相關(guān)使用python怎么編寫一個(gè)本地應(yīng)用搜索工具的內(nèi)容請(qǐng)搜索創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,!

網(wǎng)站題目:使用python怎么編寫一個(gè)本地應(yīng)用搜索工具-創(chuàng)新互聯(lián)
標(biāo)題路徑:http://www.muchs.cn/article40/deojeo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、虛擬主機(jī)、網(wǎng)站維護(hù)、服務(wù)器托管建站公司、全網(wǎng)營(yíng)銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)