Creating a webbrowser with Python and PyQT (tutorial)

Standard

In this tutorial we will build a webbrowser with Python. We will use PyQT.  Install the required packages:

If you have not done our first tutorial, you could try it. If python-kde4 cannot be found update your repository to find it. If you are on Ubuntu or Debian Linux use this link.

Creating the UI with PyQT
Start qt4-designer from your applications menu. The QT Designer application will appear:

QT_Designer

QT_Designer

Select Main Window and press Create. We now have our designer window open.  Drag a KWebView component on the window. If you have a QtWebView in the component list. use that instead. We also add an Line Edit on top. Press File > Save As > browser.ui.  Run the command:

This will generate a Python file. Remove the line “from kwebview import KWebView” from the bottom of the browser.py file. Change KWebView to QtWebView. We want to use QtWebView instead. If you are lazy to change that, take the browser.py file from below.

Creating the logic.
Create a file called run.py with this contents:

This code will use the UI as defined in browser.py and add logic to it. The lines

The first line defines the callback or event. If a person presses enter (returnPressed), it will call the function loadURL. It makes sure that once you press enter, the page is loaded with that function. If you did everything correctly, you should be able to run the browser with the command:

Please make sure you type the full url, e.g.  : https://talkera.org including the http:// part.  Your browser should now start:

browser

If your code does not run, please use the codes below (or look at the differences and change whats wrong):

browser.py

run.py

Leave a Reply