Skip to content
Advertisement

Why my PyQt5 Webview Code is not working?

This is my code. Why its not working? Where is my problem?

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

Advertisement

Answer

Your code has 2 problems:

  1. The QWebEngineView is not a child of the window so it will not be displayed. Change to self.widget = QWebEngineView(Form)

  2. QUrl("google.com") is not a valid url so you have 2 options, change to:

    • QUrl("https://google.com") OR
    • QUrl.fromUserInput("google.com")
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement