温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用python怎么编写一个本地应用搜索工具

发布时间:2021-03-01 15:20:06 来源:亿速云 阅读:233 作者:戴恩恩 栏目:开发技术

这篇文章主要介绍了使用python怎么编写一个本地应用搜索工具,此处通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考价值,需要的朋友可以参考下:

python可以做什么

Python是一种编程语言,内置了许多有效的工具,Python几乎无所不能,该语言通俗易懂、容易入门、功能强大,在许多领域中都有广泛的应用,例如最热门的大数据分析,人工智能,Web开发等。

设计思路

使用python怎么编写一个本地应用搜索工具

四.源代码

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

 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

到此这篇关于使用python怎么编写一个本地应用搜索工具的文章就介绍到这了,更多相关使用python怎么编写一个本地应用搜索工具的内容请搜索亿速云以前的文章或继续浏览下面的相关文章希望大家以后多多支持亿速云!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI