我已经创建了一个tkinter应用程序,在该应用程序上有一个提交按钮,当我按下该按钮时,爬网请求将转到request.get(url)方法,并且从爬网开始,并且tkinter变得不活动,直到它给我响应为止。但是我想要tkinter应用程序中的另一个按钮“停止”,当按下该按钮时,该过程将在后台停止,下次,我希望我可以按下提交按钮以再次启动该过程。
我尝试了tkinter中可用的destroy和quit方法,但是它们关闭了tkinter小部件本身。
我的代码是:-
#!/usr/bin/python
# -*- coding: latin-1 -*-
from Tkinter import *
import thread
from tool_crawler import *
import tkMessageBox
import ttk
# function which accepts url from GUI and pass it further for the
def crawl(url):
time_delay = time_delay_box.get()
if url == "Please enter URL.......":
tkMessageBox.showerror("Error(错误)", "Please enter a valid URL(请输入有效网址)")
elif not url:
tkMessageBox.showerror("Error(错误)", "Please enter a valid URL(请输入有效网址)")
else:
tree.heading("#0", text=url)
links = find_links(url, time_delay)
#print links
for i, link in enumerate(links):
tree.insert("", i, str(i), text=link)
tree.bind("<Double-1>", OnDoubleClick)
# function will invoke when any node in tree is double clicked
def OnDoubleClick(event):
item = tree.identify('item',event.x,event.y)
url = tree.item(item,"text")
index = tree.index(item)
parent = tree.parent(item)
crawl_child(url,index,parent)
def crawl_child(url,index,parent):
links = find_links(url, time_delay)
#print links
for i, link in enumerate(links):
if not parent:
tree.insert(str(index), i, str(index)+"."+str(i), text=link)
else:
tree.insert(str(parent)+"."+str(index), i, str(parent)+"."+str(index)+"."+str(i), text=link)
# tkinter object
top = Tk()
top.configure(background='Lavender')
# window title
top.title("Fan Wan Crawler ")
# tkinter minimum and maximum size
top.minsize(width=1460, height=800)
top.maxsize(width=1460, height=800)
# url entry area i.e. text box to enter the URLS
url = Entry(top, bd =3, width = 180)
url.insert(0, "Please enter URL.......")
url.pack(side = TOP)
# function to show and hide the text on entry and exit
def default(event):
current = url.get()
if current == "Please enter URL.......":
url.delete ("0", END)
elif not current:
url.insert("0", "Please enter URL.......")
# code to call function default on focusin and focusout
url.bind("<FocusIn>", default)
url.bind("<FocusOut>", default)
# submit button which is performing action on submit
submit = Button(top, text="Submit(提交)", width=15, bg='lightblue', command=lambda: crawl(url.get()))
submit.pack(side = TOP)
# time delay label
time_label = Label(top, text="Time Dealy (时间延迟):", font= ("Helvetica", 12), bg="Lavender", fg = "green")
time_label.place(x=2, y=27)
# time delay Entry
time_delay_box = Spinbox(top, from_=0, to=100, width=3)
time_delay_box.place(x=175, y=27)
# time description
time_label = Label(top, text="(in sec.(以秒为单位))", font=("Helvetica", 12), bg="Lavender", fg = "green")
time_label.place(x=220, y=27)
# tree area
tree = ttk.Treeview(top, selectmode="browse", height= "36")
columns = tree.column("#0", minwidth=0, width=720, stretch=True)
tree.place(x=2,y=56)
top.mainloop()
基本上我正在调用sepearte文件中存在的find_links函数并具有request.get函数来爬取链接,目前我不使用thread.So,有没有办法在不使用线程的情况下停止此过程。我不想暂停它,我只是想杀死这个过程。
最佳答案
您可以使用thread
在另一个线程中开始抓取/爬取,然后创建一个暂停/继续按钮,通过线程方法threading.Condition
和threading.Lock()
启用“暂停”和“取消暂停”生成的线程。
设置线程,不创建GUI或其他任何方法的示例仅显示了如何在GUI中单击按钮来使用可暂停线程的示例,因为选择设置/合并线程的方式将有所不同。这假定在线程完成处理当前request.get
调用之后将发生暂停和恢复。尝试使线程停止在任务中间是一个完全不同的问题。
import threading, requests
import tkinter as tk
def switch_thread_states(widget):
#or whatever pause / resume text you wish for the widget / button
if widget['text'] == 'Pause'
widget['text'] = 'Resume'
else:
widget['text'] = 'Pause'
for thread in threading.enumerate():
if not isinstance(thread, threading._MainThread):
if thread.paused:
thread.resume()
else:
thread.pause()
class PauseableThread(threading.Thread):
def __init__(self, urls):
threading.Thread.__init__(self)
self.urls = urls
self.paused = False
self.pause_cond = threading.Condition(threading.Lock())
def run(self):
for url in self.urls:
with self.pause_cond:
while self.paused:
self.pause_cond.wait()
#make requests here or whatever you're doing
requests.get(url, headers={'User-Agent':'Mozilla/5.0 .....'})
def pause(self):
self.paused = True
self.pause_cond.acquire()
def resume(self):
self.paused = False
self.pause_cond.notify()
self.pause_cond.release()
#make root
#make a button that calls switch_thread_states on press
#spawn threads with urls lists etc
关于python - 如何停止python中的request.get,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36416327/