Python PyQt5 GUI Script to Build Multi Tabbed Web Browser Using PyQtWebEngine Library Full Project For Beginners

Python PyQt5 GUI Script to Build Multi Tabbed Web Browser Using PyQtWebEngine Library Full Project For Beginners


Welcome folks today in this blog post we will be building a very basic web browser in python using the PyQt5 library. All the full source code of the application will be given below




Get Started



In order to get started we need to install the following libraries using the pip command as shown below


pip install pyqt5


pip install pyqtwebengine


After installing all these libraries make an file and copy paste the following code




# importing required libraries 
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtPrintSupport import *
import os 
import sys 

# main window 
class MainWindow(QMainWindow): 

    # constructor 
    def __init__(self, *args, **kwargs): 
        super(MainWindow, self).__init__(*args, **kwargs) 

        # creating a tab widget 
        self.tabs = QTabWidget() 

        # making document mode true 

        # adding action when double clicked 

        # adding action when tab is changed 

        # making tabs closeable 

        # adding action when tab close is requested 

        # making tabs as central widget 

        # creating a status bar 
        self.status = QStatusBar() 

        # setting status bar to the main window 

        # creating a tool bar for navigation 
        navtb = QToolBar("Navigation") 

        # adding tool bar tot he main window 

        # creating back action 
        back_btn = QAction("Back", self) 

        # setting status tip 
        back_btn.setStatusTip("Back to previous page") 

        # adding action to back button 
        # making current tab to go back 
        back_btn.triggered.connect(lambda: self.tabs.currentWidget().back()) 

        # adding this to the navigation tool bar 

        # similarly adding next button 
        next_btn = QAction("Forward", self) 
        next_btn.setStatusTip("Forward to next page") 
        next_btn.triggered.connect(lambda: self.tabs.currentWidget().forward()) 

        # similarly adding reload button 
        reload_btn = QAction("Reload", self) 
        reload_btn.setStatusTip("Reload page") 
        reload_btn.triggered.connect(lambda: self.tabs.currentWidget().reload()) 

        # creating home action 
        home_btn = QAction("Home", self) 
        home_btn.setStatusTip("Go home") 

        # adding action to home button 

        # adding a separator 

        # creating a line edit widget for URL 
        self.urlbar = QLineEdit() 

        # adding action to line edit when return key is pressed 

        # adding line edit to tool bar 

        # similarly adding stop action 
        stop_btn = QAction("Stop", self) 
        stop_btn.setStatusTip("Stop loading current page") 
        stop_btn.triggered.connect(lambda: self.tabs.currentWidget().stop()) 

        # creating first tab 
        self.add_new_tab(QUrl(''), 'Homepage') 

        # showing all the components 

        # setting window title 
        self.setWindowTitle("Geek PyQt5") 

    # method for adding new tab 
    def add_new_tab(self, qurl = None, label ="Blank"): 

        # if url is blank 
        if qurl is None: 
            # creating a google url 
            qurl = QUrl('') 

        # creating a QWebEngineView object 
        browser = QWebEngineView() 

        # setting url to browser 

        # setting tab index 
        i = self.tabs.addTab(browser, label) 

        # adding action to the browser when url is changed 
        # update the url 
        browser.urlChanged.connect(lambda qurl, browser = browser: 
                                self.update_urlbar(qurl, browser)) 

        # adding action to the browser when loading is finished 
        # set the tab title 
        browser.loadFinished.connect(lambda _, i = i, browser = browser: 

    # when double clicked is pressed on tabs 
    def tab_open_doubleclick(self, i): 

        # checking index i.e 
        # No tab under the click 
        if i == -1: 
            # creating a new tab 

    # wen tab is changed 
    def current_tab_changed(self, i): 

        # get the curl 
        qurl = self.tabs.currentWidget().url() 

        # update the url 
        self.update_urlbar(qurl, self.tabs.currentWidget()) 

        # update the title 

    # when tab is closed 
    def close_current_tab(self, i): 

        # if there is only one tab 
        if self.tabs.count() < 2: 
            # do nothing 

        # else remove the tab 

    # method for updating the title 
    def update_title(self, browser): 

        # if signal is not from the current tab 
        if browser != self.tabs.currentWidget(): 
            # do nothing 

        # get the page title 
        title = self.tabs.currentWidget().page().title() 

        # set the window title 
        self.setWindowTitle("% s - Geek PyQt5" % title) 

    # action to go to home 
    def navigate_home(self): 

        # go to google 

    # method for navigate to url 
    def navigate_to_url(self): 

        # get the line edit text 
        # convert it to QUrl object 
        q = QUrl(self.urlbar.text()) 

        # if scheme is blank 
        if q.scheme() == "": 
            # set scheme 

        # set the url 

    # method to update the url 
    def update_urlbar(self, q, browser = None): 

        # If this signal is not from the current tab, ignore 
        if browser != self.tabs.currentWidget(): 


        # set text to the url bar 

        # set cursor position 

# creating a PyQt5 application 
app = QApplication(sys.argv) 

# setting name to the application 
app.setApplicationName("Coding Shiksha PyQt5") 

# creating MainWindow object 
window = MainWindow() 

# loop 


See also  Python 3 (OpenCV + Numpy + Qrcode) Example Script to Scan or Read Qr Codes and Generate QR Codes Full Example Project For Beginners


Now when you execute the python script by typing the below command






Leave a Reply