为什么我的 PyQt5 Webview 代码不工作?

Why my PyQt5 Webview Code is not working?

这是我的代码。为什么它不起作用? 我的问题在哪里?

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtWidgets import QApplication



class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(1280, 960)
        self.widget = QWebEngineView()
        self.widget.setGeometry(QtCore.QRect(0, 0, 1270, 920))
        self.widget.setObjectName("widget")
        self.widget.load(QUrl("google.com"))
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        
 
if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

您的代码有 2 个问题:

  1. QWebEngineView 不是 window 的子项,因此不会显示。更改为 self.widget = QWebEngineView(Form)

  2. QUrl("google.com") 不是有效的 url 所以你有 2 个选项,更改为:

    • QUrl("https://google.com")
    • QUrl.fromUserInput("google.com")