In this tutorial we will build a webbrowser with Python. We will use the PyQT library which has a web component. In this tutorial you will learn how to link all the components together. We will use the default rendering engine and not roll one in this tutorial.
Start qt4-designer from your applications menu. The QT Designer application will appear:
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 (qtwebkit) in the component list. use that instead. We also add an Line Edit on top. Press File > Save As > browser.ui. Run the command:
pyuic4 browser.ui > browser.py
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.
Create a file called run.py with this contents:
import sys from browser import BrowserDialog from PyQt4 import QtCore, QtGui from PyQt4.QtCore import QUrl from PyQt4.QtWebKit import QWebView
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://pythonspot.com including the http:// part. Your browser should now start:
Leave a Reply:
Thank you for the tutorial. Could you please teach us how to use PyOtherSide with QtQuick as well?
Great tutorial! It helped me a lot, but I still need one more thing. I was wondering is there a way to know the current URL of the page I'm visiting? For my project I need to know the URL of the page I'm currently on. Thank you!