主页 > 知识库 > python 制作本地应用搜索工具

python 制作本地应用搜索工具

热门标签:硅谷的囚徒呼叫中心 Win7旗舰版 客户服务 语音系统 电话运营中心 百度AI接口 企业做大做强 呼叫中心市场需求

一.准备工作

请确保已经安装tkinter、pyperclip、threading

二.预览

1.启动

这是程序启动的主界面。

2.运行

搜索之后的界面。

3.结果

选择应用,右击鼠标复制它的下载链接。

三.设计思路

四.源代码

本次还是将GUI和搜索引擎分离开来,只要下面两个py文件在一个文件夹,结合已有的数据库就能实现上述功能。

4.1 GUI.py

from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from Search_Apps import Find_APP
import threading
import pyperclip
'''

-treeview显示搜索结果
-Menu绑定复制链接

'''
class App:
 def __init__(self):
  self.w=Tk()
  self.w.title('应用搜索工具(本地版)-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='关键字:')
  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='关于',menu=self.s2)
  self.s1.add_command(label='搜索',command=lambda :self.thread_it(self.search_app))
  self.s1.add_command(label='复制下载地址',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='联系作者',command=self.show_info)
  self.w.protocol('WM_DELETE_WINDOW',self.quit_window)
  self.m2.add_command(label='复制链接',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数据
  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)}]个关于[{key_word}]的应用')
   elif self.data is False:
    self.l2.config(background='red')
    self.l2_var.set(f'数据库连接失败,请检查数据库配置!')
   else:
    self.l2.config(background='green')
    self.l2_var.set(f'没有检索到关于[{key_word}]的应用')
  else:
   messagebox.showwarning('警告','请输入关键字!')
   self.l2.config(background='red')
   self.l2_var.set(f'请输入关键字!')

 def display_infos(self,event):
  #获取treeview当前选中项数据
  curr=self.tree.item(self.tree.focus()).get('values')
  #获取treeview当前选中项索引
  # 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('联系作者', '作者QQ:xxxx')

 def show_explain(self):
  messagebox.showinfo('说明', '\r本软件仅供学习,请勿用于商业用途\n\n1.在输入框输入关键字进行搜索\n2.选择应用右击提取下载地址')

 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('提示','下载地址已成功复制到剪切板!')
  except AttributeError:
   messagebox.showwarning('警告','请先选中应用!')
   self.l2.config(background='red')
   self.l2_var.set('请先选中应用!')

 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()

4.2 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

五.总结

本次使用tkinter制作了一款应用搜索工具,使用其中的Treeview显示搜索结果,使用Menu绑定相关操作,通过执行Mongodb查询语句得到相关数据,所以要结合本地Mongodb数据库,整体执行效率高于网络接口访问,有机会的话,可能再发一篇通过接口得到数据的GUI,思路、代码方面有什么不足欢迎各位大佬指正、批评!

以上就是python 制作本地应用搜索工具的详细内容,更多关于python 制作应用搜索工具的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
  • Python基于爬虫实现全网搜索并下载音乐
  • Python实现中英文全文搜索的示例
  • python搜索算法原理及实例讲解
  • Python大批量搜索引擎图像爬虫工具详解
  • Python利用Faiss库实现ANN近邻搜索的方法详解
  • 利用python对mysql表做全局模糊搜索并分页实例
  • Python爬虫爬取百度搜索内容代码实例
  • python爬虫开发之使用python爬虫库requests,urllib与今日头条搜索功能爬取搜索内容实例
  • python实现全排列代码(回溯、深度优先搜索)
  • python采集百度搜索结果带有特定URL的链接代码实例
  • python 制作磁力搜索工具

标签:长沙 安康 济南 崇左 山西 山西 喀什 海南

巨人网络通讯声明:本文标题《python 制作本地应用搜索工具》,本文关键词  ;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 收缩
    • 微信客服
    • 微信二维码
    • 电话咨询

    • 400-1100-266