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:
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 (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.
QWebView exploration
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:
python run.py
Please make sure you type the full url, e.g. : https://pythonspot.com including the http:// part. Your browser should now start:
If your code does not run, please use the codes below (or look at the differences and change whats wrong):
browser.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'browser.ui' # # Created: Fri Jan 30 20:49:32 2015 # by: PyQt4 UI code generator 4.10.4 # # WARNING! All changes made in this file will be lost!
import sys from PyQt4 import QtCore, QtGui from PyQt4.QtGui import QApplication from PyQt4.QtCore import QUrl from PyQt4.QtWebKit import QWebView
try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def_fromUtf8(s): return s
Thank you for the tutorial. Could you please teach us how to use PyOtherSide with QtQuick as well?
Sasha•Sat, 13 Feb 2016
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!
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!