Creating a web browser with Python and PyQt

Creating a web browser with Python and PyQt

  1. Python
At this tutorial we create own web browser, using Python. We also will be using PyQt library. To start we need to install next packages.

sudo pip install python-qt4
sudo apt-get install qt4-designer
sudo apt-get install pyqt4-dev-tools
sudo apt-get install python-kde4

 

If python-kde4 don’t want to install, then update your repository.

Creating graphics interface with PyQt

Locate and open the qt4-designer. Select “Main Window” and click “Create”. And now we have interface window, what will  be our graphics interface. Drag component KWebView on our window. If in your list will be only QtWebView  component then select him. Also add componet – Line Edit the upper area of the window. Click File > Save As > browser.ui. Run command.

pyuic4 browser.ui > browser.py

 

This command was generates Python file. Remove from kwebview import KWebView line from the bottom of browser.py file. Change KWebView  on QtWebView. We will be using QtWebView instead KWebView.

Creating logic

Create  run.py file with the following contents:

import sys
from browser import BrowserDialog
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import QUrl
from PyQt4.QtWebKit import QWebView

class MyBrowser(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        QWebView.__init__(self)
        self.ui = BrowserDialog()
        self.ui.setupUi(self)
        self.ui.lineEdit.returnPressed.connect(self.loadURL)

    def loadURL(self):
        url = self.ui.lineEdit.text()
        self.ui.qwebview.load(QUrl(url))
        self.show()  
        #self.ui.lineEdit.setText("")

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyBrowser()
    myapp.ui.qwebview.load(QUrl('https://www.talkera.org/python'))
    myapp.show()
    sys.exit(app.exec_())

This code uses a graphical interface described in the file browser.py and adds logic to it. Lines:

self.ui.lineEdit.returnPressed.connect(self.loadURL)

    def loadURL(self):
        url = self.ui.lineEdit.text()
        self.ui.qwebview.load(QUrl(url))
        self.show()  
        #self.ui.lineEdit.setText("")

First line describe action. If user click enter (returnPressed), this call loadURL function. That means that in any case, when you press enter page load according to the above functions. if you all make correct, then browser must to run with next command.

python run.py

Please ensure that you use the full url: http://talkera.org including the prefix http: //. Your browser must activate .

If your code does not run, use the following code (or check and find differences): 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

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class BrowserDialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(1024, 768)
        self.qwebview = QWebView(Dialog)
        self.qwebview.setGeometry(QtCore.QRect(0, 50, 1020, 711))
        self.qwebview.setObjectName(_fromUtf8("kwebview"))
        self.lineEdit = QtGui.QLineEdit(Dialog)
        self.lineEdit.setGeometry(QtCore.QRect(10, 20, 1000, 25))
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Browser", "Browser", None))

run.py

import sys
from browser import BrowserDialog
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import QUrl
from PyQt4.QtWebKit import QWebView

class MyBrowser(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        QWebView.__init__(self)
        self.ui = BrowserDialog()
        self.ui.setupUi(self)
        self.ui.lineEdit.returnPressed.connect(self.loadURL)

    def loadURL(self):
        url = self.ui.lineEdit.text()
        self.ui.qwebview.load(QUrl(url))
        self.show()  
        #self.ui.lineEdit.setText("")

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyBrowser()
    myapp.ui.qwebview.load(QUrl('https://www.talkera.org/python'))
    myapp.show()
    sys.exit(app.exec_())

Thanks you all!!