为什么我的PyQT5WebView代码不起作用?

wtzytmuj  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(424)

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

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_())
o0lyfsai

o0lyfsai1#

您的代码有两个问题:
qwebengineview不是窗口的子窗口,因此不会显示它。改为
self.widget = QWebEngineView(Form) QUrl("google.com") 不是有效的url,因此您有2个选项,请更改为: QUrl("https://google.com")QUrl.fromUserInput("google.com")

相关问题