PyQt5 WebEngine does not display pages like angular.dev or react.dev properly

49 views Asked by At

I can't figure out how to make pages like react.dev, angular.dev, facebook.com, or chat.openai load properly.

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

# Define a class MyWebBrowser that inherits from QMainWindow
class MyWebBrowser(QMainWindow):
    def __init__(self, *args, **kwargs):
        # Call the constructor of the base class (QMainWindow)
        super(MyWebBrowser, self).__init__(*args, **kwargs)

        # Create the main window and set its title
        self.window = QWidget()

        # Create a horizontal layout for the main window
        self.layout = QHBoxLayout(self.window)
        self.layout.setContentsMargins(0, 0, 0, 0)

        # Create a QWebEngineView widget for displaying web content
        self.browser = QWebEngineView()
        self.layout.addWidget(self.browser)

        # Access the settings of the web engine
        self.settings = self.browser.settings()

        # Enable JavaScript in the web engine
        self.settings.setAttribute(QWebEngineSettings.JavascriptEnabled, True)

        # Set the initial URL to "https://react.dev"
        self.browser.setUrl(QUrl("https://react.dev"))

        # Define a mobile user agent string
        self.mobile_user_agent = (
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
        )

        # Set the user agent for the web page
        self.browser.page().profile().defaultProfile().setHttpUserAgent(self.mobile_user_agent)

        # Set the layout for the main window
        self.window.setLayout(self.layout)

        # Set the default size for the main window
        self.defaultSize = QSize(500, 800)
        self.window.setFixedSize(self.defaultSize)

        # Show the main window
        self.window.show()

# Create a QApplication instance
app = QApplication([])

# Create an instance of MyWebBrowser
window = MyWebBrowser()

# Start the application event loop
app.exec_()

I hope the mentioned pages load and function properly; for instance, the button to switch to the dark theme on React doesn't work, or the background of Angular doesn't display.

0

There are 0 answers