PyQt5在JS window.open上打开新窗口并带有属性

时间:2019-05-28 12:01:27

标签: python pyqt pyqt5 qtwebengine qwebengineview

当在HTML视图中有window.open时,我需要在单击时打开一个新窗口... 它确实按照我的代码打开。但是当有带有属性的window.open时,它将打开一个新窗口一次,然后不再发生

我尝试了不同的事情

from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
from PyQt5.QtCore import QUrl
from PyQt5 import QtGui
from PyQt5.QtPrintSupport import QPrinter 
from PyQt5.QtCore import *

from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton, 
                             QToolTip, QMessageBox, QLabel,QMenu,QKeySequenceEdit)



class MyPage(QtWebEngineWidgets.QWebEnginePage):
     def triggerAction(self, action, checked=False):
        if action == QtWebEngineWidgets.QWebEnginePage.OpenLinkInNewWindow:
            self.createWindow(
                QtWebEngineWidgets.QWebEnginePage.WebBrowserWindow
            )
        return super(MyPage, self).triggerAction(action, checked)



class MyWindow(QtWebEngineWidgets.QWebEngineView):
    currentLoadProgress = 0
    currentFile = ''
    pdf_count = 0

    def __init__(self,windows, parent=None):
        super(MyWindow, self).__init__()
        self.myPage = MyPage(self)
        self.setPage(self.myPage)
        self._windows = windows
        self._windows.append(self)
        self.resize(640, 480)
        self.load(QtCore.QUrl.fromUserInput("file:///C:/Users/User/Desktop/default.html"))
        self.show()


    def createWindow(self, windows):
        if windows == QtWebEngineWidgets.QWebEnginePage.NavigationTypeLinkClicked:
            webview = MyWindow(self._windows)
            webview.show()
            return webview
        if windows == QtWebEngineWidgets.QWebEnginePage.WebDialog:
            webview = MyWindow([])
            webview.show()
            return webview
        return super(MyWindow, self).createWindow(windows)



if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    windows = []
    a= MyWindow(windows)
    a.show()
    sys.exit(app.exec_())
<html>

<head>

<title>URL Error</title>

</head>

<body>

<center style="padding-top:100px">

<h2>Invaid URL</h2>

<p>No URL passed in argument!</p>


</center>


<a href="#" onclick="return openPopup('1');">JS function window.open with Properties</a><br><br>


<script language="javascript">

function openPopup(obj)

{

try

{

if(obj=="1")

{

window.open('https://www.google.com','DevSearch','top=0,left=0,width=950,height=550,status=yes,resizable=yes,scrollbars=yes'); 

}
}  

catch(e)

{

alert("Script Error... Please Contact With Administrator ! ");

return false;

}

}

</script>

</body>

</html>

使用window单击该链接。打开带有属性的链接时,每次单击该链接时,都会在新选项卡中打开该链接。

0 个答案:

没有答案