不知不觉已经到了 大年三十,在此祝大家 牛年大吉,恭喜发财!今天还是要发一篇博客,是关于tkinter的一款磁力搜索GUI工具,可以帮助我们检索网络 资源。
一.准备工作
装库,主要有tkinter os threading win32 pillow
二.预览
预览我们的软件
1.启动
2.运行
3.结果
可以选择保存链接到本地,也可以选择复制链接倒剪切板。
3.1保存到本地:
保存格式为txt
3.2复制链接:
复制以后,如果存在第三方下载工具,就能在工具中打开。
三.tk源代码
设计流程就不说了,本次设计主要在界面布局上。这里直接上源代码。
#coding:utf-8
from tkinter import ttk
from tkinter import *
from tkinter import messagebox
from tkinter.filedialog import askdirectory
import tkinter.simpledialog
from PIL import Image,ImageTk
import os
import threading
import win32clipboard as w
from Spiders.spider1 import magent_spider1
from Spiders.spider2 import magent_spider2
from Spiders.spider3 import magent_spider3
from Spiders.spider4 import magent_spider4
from QQ_infos import QQ_data
#spider1和spider4比较稳定
imgs=['lsj.png']
class App:
def __init__(self):
self.window=Tk()
self.window.resizable(0,0)
width=420
height=400
screen_width=self.window.winfo_screenwidth()
screen_height=self.window.winfo_screenheight()
left=(screen_width-width)/2
top=(screen_height-height)/2
self.window.geometry('%dx%d+%d+%d'%(width,height,left,top))
self.window.title('老司机搜索器-v1.0')
self.create_widget()
self.place_sidget()
self.set_config()
self.window.mainloop()
def create_widget(self):
self.l1=ttk.Label(self.window,text='选择引擎:')
self.r_chose=StringVar()
self.filepath=StringVar()
self.ls_var=StringVar()
self.l5_var=StringVar()
self.l_p_var=StringVar()
self.qq_screenname_var=StringVar()
self.r1=Radiobutton(self.window,text='引擎一',value='1',variable=self.r_chose)
self.r2=Radiobutton(self.window,text='引擎二',value='2',variable=self.r_chose)
self.r3=Radiobutton(self.window,text='引擎三',value='3',variable=self.r_chose)
self.r4=Radiobutton(self.window,text='引擎四',value='4',variable=self.r_chose)
self.l2=ttk.Label(self.window,text='关键字:')
self.e1=ttk.Entry(self.window)
self.b1=ttk.Button(self.window,text='搜索',)
self.l3 = ttk.Label(self.window, text='存储路径:')
self.e2 = ttk.Entry(self.window,textvariable=self.filepath)
self.b2 = ttk.Button(self.window, text='选择目录')
self.qq_screenname=ttk.Label(self.window,textvariable=self.qq_screenname_var)
self.ls=Label(self.window,bg='pink',textvariable=self.ls_var)
self.listbox=Listbox(self.window,selectmode=SINGLE)
self.S_coll_vertical = Scrollbar(self.window, orient=VERTICAL)
self.S_coll_level = Scrollbar(self.window, orient=HORIZONTAL)
self.b3=ttk.Button(self.window,text='保存')
self.paned = PanedWindow(self.window)
self.img=imgs
img = Image.open(self.img[0])
photo = img.resize((180, 190))
self.paned.image = ImageTk.PhotoImage(photo)
self.l4=Label(self.window,image=self.paned.image,)
self.b4=ttk.Button(self.window,text='上一页')
self.l_p=ttk.Label(self.window,textvariable=self.l_p_var,background='orange')
self.b5=ttk.Button(self.window,text='下一页')
self.l5=ttk.Label(self.window,text='影片信息:',)
self.m=Menu(self.window)
self.s1=Menu(self.m,tearoff=False)
self.s2=Menu(self.m,tearoff=False)
self.s3=Menu(self.m,tearoff=False)
def place_sidget(self):
self.l1.place(x=20,y=12)
self.r1.place(x=100,y=10)
self.r2.place(x=180,y=10)
self.r3.place(x=260,y=10)
self.l2.place(x=20,y=45)
self.e1.place(x=90,y=40,height=30,width=120)
self.b1.place(x=230,y=40,width=80)
self.l3.place(x=20,y=85)
self.e2.place(x=90,y=80,height=30,width=120)
self.b2.place(x=230,y=80,width=80)
self.ls.place(x=0,y=114,width=420,height=22)
self.listbox.place(x=20,y=140,width=190,height=180)
self.b3.place(x=230,y=140,width=80)
self.S_coll_vertical.place(x=200, y=140,width=15,height=180)
self.S_coll_level.place(x=20, y=310,width=190,height=15)
self.paned.place(x=230,y=170,height=180,width=300)
self.b4.place(x=15,y=328,width=60)
self.l_p.place(x=85,y=331,)
self.b5.place(x=145,y=328,width=60)
self.l4.place(x=215,y=170,height=200,width=220)
self.l5.place(x=0,y=365,height=25,width=420)
def set_config(self):
self.lock=True
self.ls_var.set('---还未搜索---')
self.b1.config(command=self.search_res)
self.r_chose.set(1)
self.S_coll_vertical.config(command=self.listbox.yview)
self.listbox['yscrollcommand'] = self.S_coll_vertical.set
self.S_coll_level.config(command=self.listbox.xview)
self.listbox['xscrollcommand'] = self.S_coll_level.set
self.window['menu']=self.m
self.s1.add_command(label='选择目录',command=self.open_file_savepath)
self.s1.add_command(label='打开文件夹',command=self.open_dir)
self.s1.add_separator()
self.s1.add_command(label='退出',command=self.quit_window)
self.m.add_cascade(label='文件',menu=self.s1)
self.m.add_cascade(label='操作',menu=self.s2)
self.m.add_cascade(label='关于',menu=self.s3)
self.s2.add_command(label='搜索',command=lambda :self.thread_it(self.search_res))
self.s2.add_command(label='复制',command=lambda :self.thread_it(self.copy_magent))
self.s2.add_command(label='保存',command=lambda :self.thread_it(self.save_magent))
self.s3.add_command(label='联系作者',command=self.show_QQ)
self.s3.add_command(label='解锁隐藏引擎',command=self.unlock_engine)
self.b1.config(command=lambda :self.thread_it(self.search_res))
self.b3.config(command=lambda :self.thread_it(self.save_magent))
self.b4.config(command=lambda :self.thread_it(self.page_min))
self.b5.config(command=lambda :self.thread_it(self.page_add))
self.l5.config(textvariable=self.l5_var,background='#008080')
self.b2.config(command=self.open_file_savepath)
self.l_p_var.set('页码:')
self.listbox.bind('<<ListboxSelect>>',self.display_infos)
# 绑定esc键---退出
self.window.bind('<Escape>', self.escape)
# 使用return键给输入框Entry绑定enter事件---search搜索
self.e1.bind('<Return>', self.enter)
self.window.protocol('WM_DELETE_WINDOW',self.quit_window)
def enter(self):
self.search_res()
def escape(self,event):
self.quit_window()
def quit_window(self):
ask=messagebox.askyesno('退出','真的要退出吗?')
if ask:
self.window.destroy()
def open_file_savepath(self):
self.file = askdirectory()
self.filepath.set(self.file)
abs_path = os.path.abspath(self.filepath.get())
self.magnetic_dir = abs_path
def open_dir(self):
os.startfile(self.magnetic_dir)
def show_QQ(self):
messagebox.showinfo('联系作者','作者QQ:xxxxxxxxx')
def search_res(self):
key_word=self.e1.get()
self.ls_var.set('')
self.page=1
if key_word:
# print(self.r_chose.get())
self.listbox.delete(0,END)
#使用self.r_chose.get()判断选中了哪个
if self.r_chose.get()=='1':
spider1=magent_spider1()
self.speed,self.flag,self.result_list= spider1.get_data(key_word,1)
if self.speed is None:
messagebox.showinfo('提示','此引擎停用!')
self.r1.config(state='disable')
self.ls_var.set(self.speed)
self.l_p_var.set('页码:1')
for data in self.result_list:
title = data.split(')-*/(')[0]
self.listbox.insert(END, title)
elif self.r_chose.get()=='2':
spider2=magent_spider2()
self.speed,self.flag,self.result_list=spider2.get_data(key_word,1)
if self.speed is None:
messagebox.showinfo('提示','此引擎停用!')
self.r2.config(state='disable')
else:
self.ls_var.set(self.speed)
for data in self.result_list:
title = data.split(')-*/(')[0]
self.listbox.insert(END, title)
#爬虫3只返回flag和result_list
elif self.r_chose.get()=='3':
spider3=magent_spider3()
self.flag, self.result_list = spider3.get_data(key_word, 1)
if self.flag:
self.ls_var.set(f'搜索到关于{key_word}的资源')
self.l_p_var.set('页码:1')
for data in self.result_list:
title = data.split(')-*/(')[0]
self.listbox.insert(END, title)
else:
messagebox.showinfo('提示','此引擎停用!')
self.r3.config(state='disable')
elif self.r_chose.get()=='4':
spider4=magent_spider4()
self.speed,self.flag,self.result_list=spider4.get_data(key_word,1)
if self.speed is None:
messagebox.showinfo('提示','此引擎停用!')
self.r4.config(state='disable')
else:
self.l_p_var.set('页码:1')
self.ls_var.set(self.speed)
for data in self.result_list:
title = data.split(')-*/(')[0]
self.listbox.insert(END, title)
self.listbox.update()
else:
messagebox.showwarning('警告','请先输入关键字!')
def display_infos(self,event):
try:
curr_content_index=self.listbox.curselection()[0]
content=self.result_list[curr_content_index].split(']-*/[')[0]
pre_con=content.split(')-*/(')[0]
aft_con = ' 大小:' + content.split(')-*/(')[1]
if len(pre_con)>45:
#print(self.listbox[curr_content_index])
self.l5_var.set(aft_con)
else:
self.l5_var.set(pre_con+aft_con)
except IndexError:
pass
def do_turn_page(self,page):
self.listbox.delete(0, END)
key_word=self.e1.get()
if self.r_chose.get()=='1':
spider1 = magent_spider1()
self.flag=spider1.get_data(key_word, page)[1]
if self.flag:
self.result_list = spider1.get_data(key_word, page)[2]
elif self.r_chose.get()=='2':
spider2 = magent_spider2()
self.flag=spider2.get_data(key_word, page)[1]
if self.flag:
self.result_list = spider2.get_data(key_word, page)[2]
elif self.r_chose.get()=='3':
spider3 = magent_spider3()
self.flag,self.result_list=spider3.get_data(key_word, page)
elif self.r_chose.get()=='4':
spider4 = magent_spider4()
self.flag = spider4.get_data(key_word, page)[1]
if self.flag:
self.result_list = spider4.get_data(key_word, page)[2]
if self.flag:
for data in self.result_list:
title = data.split(')-*/(')[0]
self.listbox.insert(END, title)
self.listbox.update()
else:
messagebox.showinfo('提示','已经是最后一页')
def page_min(self):
if self.page==1:
messagebox.showwarning('警告','当前已经是第一页了')
else:
self.page-=1
self.l_p_var.set('页码:'+str(self.page))
self.do_turn_page(self.page)
self.listbox.update()
def page_add(self):
if self.flag is False :
messagebox.showwarning('警告','当前已经是最后一页了')
else:
self.page += 1
self.l_p_var.set('页码:'+str(self.page))
self.do_turn_page(self.page)
self.listbox.update()
def get_magnet(self):
try:
curr_content_index = self.listbox.curselection()[0]
hashes = self.result_list[curr_content_index].split(']-*/[')[-1]
file_name=self.result_list[curr_content_index].split(')-*/(')[0]
if self.r_chose.get()=='1':
spider1=magent_spider1()
magnet=spider1.get_megent(hashes)
return file_name, magnet
elif self.r_chose.get() == '2':
spider2 = magent_spider2()
magnet = spider2.get_magent(hashes)
return file_name, magnet
elif self.r_chose.get() == '3':
spider3 = magent_spider3()
magnet = spider3.get_magent(hashes)
return file_name, magnet
elif self.r_chose.get() == '4':
spider4 = magent_spider4()
magnet = spider4.get_magent(hashes)
return file_name, magnet
except IndexError:
messagebox.showwarning('警告','请先选中要下载的资源!')
def save_magent(self):
if os.path.exists(self.filepath.get()):
try:
filename, magent_link=self.get_magnet()
if '.' in filename:
filename=filename.replace('.','')
file_path=self.magnetic_dir
with open(file_path+filename+'.txt','w')as f:
f.write(magent_link)
messagebox.showinfo('提示','保存磁力链接成功!')
except TypeError:
pass
else:
messagebox.showwarning('警告', '您还未选择路径!')
def copy_magent(self):
magent_link=self.get_magnet()[1]
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardText(magent_link)
w.CloseClipboard()
messagebox.showinfo('提示','磁力链接已经复制到了剪切板!')
def unlock_engine(self):
if self.lock:
# 获取字符串(标题,提示,初始值)initialvalue初始值
qq_number = tkinter.simpledialog.askstring(title='解锁', prompt='请输入QQ号:', )
# 打印内容
qq_screenname=QQ_data(qq_number).get_screenname()
if qq_screenname:
self.qq_screenname_var.set('欢迎您:\n\n'+qq_screenname)
self.qq_screenname.config(font=(('Times',12)),foreground='Tomato')
self.qq_screenname.place(x=320,y=40)
QQ_data(qq_number).save_heade_img()
messagebox.showinfo('恭喜','解锁成功!')
self.img2=os.path.abspath('./')
self.img2+='/head_img/'+qq_number+'.jpg'
img = Image.open(self.img2)
photo = img.resize((190, 170))
self.paned.image = ImageTk.PhotoImage(photo)
self.l4 = Label(self.window, image=self.paned.image, )
self.l4.place(x=215, y=170, height=180, width=210)
self.r4.config(font=(('Times',12)),foreground='blue')
self.l4.config(background='red')
self.r4.place(x=340, y=10)
self.lock=False
else:
messagebox.showerror('错误','解锁失败!')
else:
messagebox.showwarning('警告','您已成功解锁!')
def thread_it(self,func,*args):
t=threading.Thread(target=func,args=args)
t.setDaemon(True)
t.start()
if __name__ == '__main__':
a=App()
四.总结
本次使用tkinter写了一款资源搜索工具,重点还是在界面布局,因为本人美感实在差,界面布局主要参考了