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