pyqt5 HTML窗口未显示

时间:2019-05-15 09:48:34

标签: python browser pyqt5

我编写了此代码后,没有显示窗口,但终端中显示了控制台消息

我尝试添加一些额外的pyqt5导入语句,但nothng可行

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

        return super(MyPage, self).triggerAction(action, checked)


class MyWindow(QtWebEngineWidgets.QWebEngineView):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        self.myPage = MyPage(self)

        self.setPage(self.myPage)

    def createWindow(self, windowType):
        if windowType == QWebEnginePage.WebBrowserTab:
            self.webView = MyWindow()
            self.webView.setAttribute(Qt.WA_DeleteOnClose, True)
            self.webView.show()
            return self.webView
        return super(MyWindow, self).createWindow(windowType)


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    app.setApplicationName('MyWindow')

    main = MyWindow()
    main.show()
    main.load(QUrl("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_a_target"))

    sys.exit(app.exec_())

需要知道确切的导入语句,并显示用于查看HTML的窗口。

1 个答案:

答案 0 :(得分:1)

存在一个QWebEngineView大小的错误,该错误具有可以手动设置大小的解决方案:

from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets


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):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)
        self.myPage = MyPage(self)
        self.setPage(self.myPage)

    def createWindow(self, windowType):
        if windowType == QtWebEngineWidgets.QWebEnginePage.WebBrowserTab:
            webView = MyWindow(self)
            webView.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
            webView.resize(640, 480) # <----
            webView.show()
            return webView
        return super(MyWindow, self).createWindow(windowType)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    app.setApplicationName("MyWindow")

    main = MyWindow()
    main.resize(640, 480) # <----
    main.show()
    main.load(
        QtCore.QUrl(
            "https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_a_target"
        )
    )

    sys.exit(app.exec_())