【问题标题】:Loop alway stop at the third iteration [duplicate]循环总是在第三次迭代时停止[重复]
【发布时间】:2019-06-05 00:16:18
【问题描述】:

所以,伙计们,我仍然很困惑为什么迭代总是在第三轮停止
这是我的代码:

from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEnginePage
import sys
import numpy as np
from bs4 import BeautifulSoup as soup

class Client(QWebEnginePage):
    def __init__(self,url):
        global app
        self.app = QApplication(sys.argv)
        QWebEnginePage.__init__(self)
        self.html = ""
        self.loadFinished.connect(self.on_load_finished)
        self.load(QUrl(url))
        self.app.exec_()

    def on_load_finished(self):
        self.html = self.toHtml(self.Callable)
        print("Load Finished")

    def Callable(self,data):
        self.html = data
        self.app.quit()



linkgroup = []
linkgroup.append("https://docs.python.org/3/whatsnew/3.7.html")
linkgroup.append("https://docs.python.org/3/tutorial/index.html")
linkgroup.append("https://docs.python.org/3/installing/index.html")
linkgroup.append("https://docs.python.org/3/reference/index.html")
linkgroup.append("https://docs.python.org/3/using/index.html")

for h in range(0,len(linkgroup)):
    #Setting Url
    url = linkgroup[h]
    print(url)
    print("Loop Index : " + str(h))

    client_response = Client(url)

输出是这样的

https://docs.python.org/3/whatsnew/3.7.html
Loop Index : 0
Load Finished
https://docs.python.org/3/tutorial/index.html
Loop Index : 1
Load Finished
https://docs.python.org/3/installing/index.html
Loop Index : 2

如您所见,循环的其余迭代没有执行,因为它没有显示来自客户端类的响应

【问题讨论】:

  • Client 类的构造函数创建一个新的QApplication 对象。这不是一个好主意,每个程序应该只有一个 QApplication 对象。
  • 我该怎么做?这真的是循环停止的原因吗?
  • 运行你的代码我在该行也遇到了分段错误。所以是的,这正是它停止的原因。

标签: python pyqt5


【解决方案1】:

正如上面 pschill 所说,您应该只有一个 QApplication。将它作为参数传递给构造函数怎么样?类似的东西:

from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEnginePage
import sys
import numpy as np
from bs4 import BeautifulSoup as soup

class Client(QWebEnginePage):
    def __init__(self,url,app):
        self.app = app
        QWebEnginePage.__init__(self)
        self.html = ""
        self.loadFinished.connect(self.on_load_finished)
        self.load(QUrl(url))
        self.app.exec_()

    def on_load_finished(self):
        self.html = self.toHtml(self.Callable)
        print("Load Finished")

    def Callable(self,data):
        self.html = data
        self.app.quit()

linkgroup = []
linkgroup.append("https://docs.python.org/3/whatsnew/3.7.html")
linkgroup.append("https://docs.python.org/3/tutorial/index.html")
linkgroup.append("https://docs.python.org/3/installing/index.html")
linkgroup.append("https://docs.python.org/3/reference/index.html")
linkgroup.append("https://docs.python.org/3/using/index.html")

app = QApplication(sys.argv)

for h in range(0,len(linkgroup)):
    #Setting Url
    url = linkgroup[h]
    print(url)
    print("Loop Index : " + str(h))

    client_response = Client(url, app)

【讨论】:

  • 只想让你知道,代码有效~
  • 太棒了!我会更新帖子,现在它已经过测试。
猜你喜欢
  • 2012-03-05
  • 2014-10-02
  • 1970-01-01
  • 1970-01-01
  • 2019-12-17
  • 2015-10-20
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
相关资源
最近更新 更多