不知不覺已經(jīng)到了 大年三十,在此祝大家 牛年大吉,恭喜發(fā)財!今天還是要發(fā)一篇博客,是關(guān)于tkinter的一款磁力搜索GUI工具,可以幫助我們檢索網(wǎng)絡(luò) 資源。
#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比較穩(wěn)定
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('老司機(jī)搜索器-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='關(guān)鍵字:')
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='關(guān)于',menu=self.s3)
self.s2.add_command(label='搜索',command=lambda :self.thread_it(self.search_res))
self.s2.add_command(label='復(fù)制',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='聯(lián)系作者',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('聯(lián)系作者','作者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'搜索到關(guān)于{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('警告','請先輸入關(guān)鍵字!')
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('提示','已經(jīng)是最后一頁')
def page_min(self):
if self.page==1:
messagebox.showwarning('警告','當(dāng)前已經(jīng)是第一頁了')
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('警告','當(dāng)前已經(jīng)是最后一頁了')
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('提示','磁力鏈接已經(jīng)復(fù)制到了剪切板!')
def unlock_engine(self):
if self.lock:
# 獲取字符串(標(biāo)題,提示,初始值)initialvalue初始值
qq_number = tkinter.simpledialog.askstring(title='解鎖', prompt='請輸入QQ號:', )
# 打印內(nèi)容
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()
以上就是python 制作磁力搜索工具的詳細(xì)內(nèi)容,更多關(guān)于python 磁力搜索的資料請關(guān)注腳本之家其它相關(guān)文章!