简体   繁体   English

QWebEngineView().loadFinished 后如何等待脚本完成?

[英]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.

相关问题 如何在使用 QWebEngineView 'loadFinished' 加载页面后立即更改 html 元素? - How to change html elements immediately after loading page with QWebEngineView 'loadFinished'? 在PyQt4中完成加载后如何返回值 - How to return value after loadFinished in PyQt4 如何不等待线程在Python中完成 - How NOT to wait for a thread to finish in Python 如何不等功能完成python - How to not wait for function to finish python 并行运行 Python 脚本并等待所有脚本完成,然后再执行更多并行脚本 - Run Python scripts in parallel and wait for all to finish before executing more parallel scripts 循环帮助:它不等待PyQt4中的loadFinished SIGNAL - Loop help: it doesn't wait for loadFinished SIGNAL in PyQt4 如何使用单独的线程运行多个 shell 作业并在同时执行每个作业后等待每个作业完成? - How to run multiple shell jobs using separate threads and wait for each to finish after executing each simultaneously? 如何等待生成的线程在Python中完成 - How to wait for a spawned thread to finish in Python 如何等待线程完成后再继续 - How to wait for a thread to finish before continuing 如何等到所有线程完成工作? - How to wait till all threads finish their work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM