I am trying to render websites in dark mode with QtWebEngine. I had no idea how to do this, and I found this Stack Overflow question which seemed to have the solution to my problem. This person tried to use chrome flags, and they had an accepted answer which used these flags. Below is the code that the accepted answer provided:
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
if __name__ == "__main__":
import os
import sys
os.environ[
"QTWEBENGINE_CHROMIUM_FLAGS"
] = "--blink-settings=darkMode=4,darkModeImagePolicy=2"
app = QtWidgets.QApplication(sys.argv)
# or
# args = sys.argv + ["--blink-settings=darkMode=4,darkModeImagePolicy=2"]
# app = QtWidgets.QApplication(sys.argv + args)
view = QtWebEngineWidgets.QWebEngineView()
view.load(QtCore.QUrl("https://www.google.com"))
view.show()
sys.exit(app.exec_())
Below is an image of google rendered in dark mode that worked with this code:
It does look like the operating system used is not Windows (which is what I'm using), so that could potentially be the issue.This code no longer works, and renders the webpage without dark mode as shown below:
While writing this question I also came across another Stack Overflow question asking the same thing as me. This question currently has no answers and has been on this site for 6 months. Anyone have any ideas on how to do this? I'd appreciate the help.
Edit:
After asking ChatGPT to see if it would help, I was given the following code as a result (yes I know ChatGPT is not always correct and banned on this site, I'm assuming this ban is on answering questions):
from PyQt5.QtCore import Qt, QUrl
from PyQt5.QtGui import QColor, QPalette
from PyQt5.QtWebKit import QWebSettings
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebKitWidgets import QWebView
app = QApplication([])
# Enable the Force Dark Mode flag
settings = QWebSettings.globalSettings()
settings.setAttribute(QWebSettings.ForceDarkColors, True)
# Create the web browser
browser = QWebView()
browser.load(QUrl("https://www.google.com/"))
# Set the dark color palette for the web browser
palette = QPalette()
palette.setColor(QPalette.Window, QColor(53, 53, 53))
palette.setColor(QPalette.WindowText, Qt.white)
browser.setPalette(palette)
# Show the web browser
browser.show()
# Run the application event loop
app.exec_()
This code gave me the error ModuleNotFoundError: No module named 'PyQt5.QtWebKit'
. After looking up a solution to this error I found this Stack Overflow question. Essentially:
QtWebKit got deprecated upstream in Qt 5.5 and removed in 5.6.
If this code that was produced uses chrome flags, then am I correct in assuming they can no longer be used to achive this?