[英]How to wait for scripts to finish after QWebEngineView().loadFinished?
I'm trying to render an HTML file as a PDF with PyQt5.我正在尝试使用 PyQt5 将 HTML 文件呈现为 PDF。 Unfortunately, my HTML page loads slow, but I couldn't find a way to wait for it to load.
不幸的是,我的 HTML 页面加载缓慢,但我找不到等待它加载的方法。 My question is this;
我的问题是这个; How can I make PyQt5 wait for a couple of seconds, after resuming the execution?
恢复执行后,如何让 PyQt5 等待几秒钟? Or is there a better way to wait for JS scripts to execute?
还是有更好的方法来等待 JS 脚本执行?
app = QtWidgets.QApplication(sys.argv)
loader = QtWebEngineWidgets.QWebEngineView()
loader.setZoomFactor(1)
loader.page().pdfPrintingFinished.connect(loader.close)
loader.load(QtCore.QUrl(url))
def emit_pdf(finished):
loader.page().printToPdf("test.pdf")
loader.loadFinished.connect(emit_pdf)
app.exec()
I tried using QTimer but couldn't make it work.我尝试使用 QTimer 但无法使其工作。 Here is how it looked like.
这是它的样子。
app = QtWidgets.QApplication(sys.argv)
loader = QtWebEngineWidgets.QWebEngineView()
loader.setZoomFactor(1)
loader.page().pdfPrintingFinished.connect(loader.close)
loader.load(QtCore.QUrl(url))
timer = QtCore.QTimer()
timer.setInterval(2000)
def run_emit():
loader.loadFinished.connect(emit_pdf)
timer.timeout.connect(run_emit)
timer.start()
def emit_pdf(finished):
timer.stop()
loader.page().printToPdf("test.pdf")
app.exec()
There is no Explicit wait for the final product.最终产品没有显式等待。 Instead of using
QTimer.start()
or QTimer.stop()
, QTimer.singleShot()
with a lambda function will work.代替使用
QTimer.start()
或QTimer.stop()
,带有 lambda 函数的QTimer.singleShot()
将起作用。
def emit_pdf(finished):
QTimer.singleShot(2000, lambda: loader.page().printToPdf("test.pdf"))
app.exec()
I added this in case the solution comment is lost.我添加了这个以防解决方案注释丢失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.