Quick Tutorial: PyQt 5 Browser

Dear Future Googler,

If you are reading this you’re probably trying to find out how to display webpages in PyQt5. I bet you’ve been finding a bunch of tutorials written for QtWebKit but can’t get them to work, right? Don’t worry, I was in your shoes once – I’m writing this blog to give you the help I had to give myself.

Update:

Callout from Phillip Kent in the comments:

PyQtWebEngine isn’t included in recent versions of the PyQt5 installer (I have got version 5.12.1). Additional install is required: ‘pip install PyQtWebEngine’

I’ve confirmed that the code below works after you install the PyQtWebEngine package seperately.

The main issue here is that QtWebKit was replaced by QtWebEngine in Qt 5.5, which provides chromium support, so everything else you’ve found is probably out of date. In some cases you might find instructions to install the old QtWebKit, but you don’t need to do that. With some minor changes you’re good to go with vanilla PyQt5.

Basically, for the imports, you use PyQt5.QtWebEngine and PyQt5.QtWebEngineWidgets instead. Then, for the object that shows the page you have to use QWebEngineView(). After that you should be good to go!

I have a sample below that shows how to open my blog in PyQt5.

import sys
from PyQt5.QtCore import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtWidgets import QApplication

app = QApplication(sys.argv)

web = QWebEngineView()
web.load(QUrl("https://further-reading.net"))
web.show()

sys.exit(app.exec_())

The Qt5 docs should have everything you need to get the most out of this. Happy coding!

This entry was posted in Python, tutorial and tagged , . Bookmark the permalink.

2 Responses to Quick Tutorial: PyQt 5 Browser

  1. Phillip Kent says:

    PyQtWebEngine isn’t included in recent versions of the PyQt5 installer (I have got version 5.12.1). Additional install is required: ‘pip install PyQtWebEngine’

  2. Roy says:

    Thanks for the callout Phillip!

Leave a Reply to Roy Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.