Home > Mobile >  PyQt5 QWebEngineView does not show webpage
PyQt5 QWebEngineView does not show webpage

Time:05-24

The part where webpage should be rendered gets white for a fraction of second and then gets empty enter image description here

Here is my code (basically it is https://www.pythonguis.com/examples/python-web-browser/):

from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *    
import sys

class MainWindow(QMainWindow):  
    def __init__(self, *args, **kwargs):
        super(MainWindow,self).__init__(*args, **kwargs)
        self.browser = QWebEngineView()
        self.browser.setUrl(QUrl("https://www.google.com"))

        self.setCentralWidget(self.browser)

        self.show()

app = QApplication(sys.argv)
window = MainWindow()

app.exec_()

Here is similar code, which I use for rendering html in from my local folder (also does not work - same symptoms):

from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *

import sys

class MainWindow(QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow,self).__init__(*args, **kwargs)

        self.browser = QWebEngineView()
        file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'temporary_files', "map.html"))
        self.browser.load(QUrl.fromLocalFile(file_path))
        self.setCentralWidget(self.browser)
        self.show()



app = QApplication(sys.argv)
window = MainWindow()

app.exec_()

PyQt5.15.6, python3.8, OS Ubuntu 22.04 LTS. It worked before on ubuntu 18.04, problems started after reinstalling system, although I backed up and restored virtual environment, so libraries should be the same.

CodePudding user response:

Try this:

from PyQt6.QtCore import QUrl
from PyQt6.QtWidgets import *
from PyQt6.QtGui import *
from PyQt6.QtWebEngineWidgets import *
import sys

class MainWindow(QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow,self).__init__(*args, **kwargs)
        self.browser = QWebEngineView()
        self.setCentralWidget(self.browser)
        # self.browser.setUrl(QUrl("https://www.google.com"))
        self.browser.setHtml("<html><body><h1>Hello World... Hello World</h1></body></html>")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()  # moved show outside main widget
    sys.exit(app.exec())   #  use app.exec instead of app.exec_

Try using PyQt6 instead of PyQt5. There are issues with Qt5-WebEngineWidgets on Ubuntu 22.04.

CodePudding user response:

I'm seeing the same issue in Ubuntu 22.04 but not in Ubuntu 21.10 when installing PyQtWebEngine via PIP. Try installing PyQtWebEngine via system package and running your code outside the virtual environment. In Ubuntu:

sudo apt install python3-pyqt5.qtwebengine
  • Related