【发布时间】:2019-12-03 03:07:10
【问题描述】:
单击按钮后,表单将无响应,直到解析函数完成工作。
我想将 searchAll 函数移至线程。我确实阅读了几个类似问题的答案,但我不明白如何。
class MyForm(QDialog):
def __init__(self):
super().__init__()
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.buttonOK.clicked.connect(self.searchAll)
self.show()
def searchAll(self):
sID = self.ui.txtSellerID.text()
sUrl = "https://removed.com/" + sID + "/p/?section=2&page=1"
sr = requests.get(sUrl)
soup1 = BeautifulSoup(sr.text, "html.parser")
NumberOfPagesBlock = soup1.find_all("li", class_="text-gray")
if not NumberOfPagesBlock:
QMessageBox.about(self, "Warning", "Nothing Here")
else:
items = re.compile(r'[^\d.]+')
PagesCount = -(-items // 60)
for i in range(1, int(PagesCount + 1)):
itemsIdDs = soup1.find_all("div", class_="large-single-item")
for itemsIdD in itemsIdDs:
iUrl = ("https://removed.com/" + itemsIdDs.get('data-ean') + "/s")
r = requests.get(iUrl)
soup = BeautifulSoup(r.text, "html.parser")
seller = soup.find("div", id="productTrackingParams")
title = (str(ctr) + '- ' + "Title " + str(seller.get('data-title')))
self.ui.txtDetails.appendPlainText(title)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyForm()
w.show()
sys.exit(app.exec_())
【问题讨论】:
-
很难相信这里没有适合你的东西。使用QThread() 对象。参考C++ qt5 documentation,即使你在 python 中开发。以下是一些示例:How to can I add threading to PyQt5 GUI?、PyQt5 thread example