我有一个子类QWebEnginePage
,我想覆盖该javaSrciptConfirm
函数以创建我自己的弹出窗口。它可以通过启动, 并将弹出窗口上的 OK 和 Cancel 按钮QEventLoop
的信号连接到插槽来工作。QPushButton.clicked.connect
QEventLoop.quit
我无法让它工作,因为它似乎QEventLoop
立即被完全绕过了。所以我尝试了只显示在左上角的黄色按钮,当它被点击时它应该退出QEventLoop
,然后变成红色,这仍然不起作用:
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
import sys
class MainWindow(QMainWindow):
def __init__(self,parent=None,*args,**kwargs):
QMainWindow.__init__(self,parent,*args,**kwargs)
self._webEngine = QWebEngineView(self)
self._webEngine.setPage(MyWebEnginePage(self._webEngine))
self._webEngine.setUrl(QUrl("https://www.seleniumeasy.com/test/javascript-alert-box-demo.html"))
self.setCentralWidget(self._webEngine)
self.showMaximized()
class MyWebEnginePage(QWebEnginePage):
def __init__(self,parent,*args,**kwargs):
QWebEnginePage.__init__(self,parent,*args,**kwargs)
def javaScriptConfirm(self,url,msg):
print("in here")
button = QPushButton(self.parent().window())
button.setStyleSheet("background-color: yellow")
button.show()
loop = QEventLoop(self.parent().window())
button.clicked.connect(loop.quit)
loop.exec_()
button.setStyleSheet("background-color: red")
def myExceptHook(e,v,t):
sys.__excepthook__(e,v,t)
if __name__ == "__main__":
sys.excepthook = myExceptHook
app = QApplication(sys.argv)
window = MainWindow()
app.quit()
当程序运行时,它导航到https://www.seleniumeasy.com/test/javascript-alert-box-demo.html
,然后我单击显示 JS 确认框的按钮(向下第二个按钮)。在屏幕的一角,会显示一个红色按钮。它应该是黄色的,并且只有在我单击它时才会变为红色,但从QEventLoop
不循环,它会立即退出。当我可以让按钮保持黄色直到被点击时,我可以删除它并将弹出按钮连接到QEventLoop.quit
插槽。
我尝试将loop
变量 aself.loop
设为不同的父对象QEventLoop
,将a 设为button
a self.button
,但我不确定为什么循环会立即退出。QEventLoop
为了真正循环,我需要更改什么?