Make Your Own Web Browser Using Python and PyQt5

browser in python, python browser, python project, python web browser, make browser with python,

Hello Coders, In this article, I will show you how you can create an awesome simple web browser using Python.

According to Wikipedia, A Web browser is an application software for accessing the World Wide Web. When a user requests a web page from a particular website, the web browser retrieves the necessary content from a web server and then displays the page on the user's device.

We will make our web browser with a toolkit named PyQt5. It is a cross-platform GUI toolkit. You can build an interactive desktop application easily because of the tools and simplicity provided by this library. Let's get started.

➜ Also Read: 5 Number Related Python Coding Problems With Solutions

As a beginner, you should know what you have to do and what not to do. Read This: Beginner Tips & Guide To Start Learning Python In Easy WayFollow the GUI and Back-end Implementation steps.

➦ GUI Implementation :

1. Create the main window.

2. Create a QWebEngineView object and add it as the central widget to the main window.

3. Add Status bar to the main window.

4. Create a toolbar and add navigation buttons and the line edit to show the URL, the toolbar will look like below:

➦ Back-End Implementation:

1. Add update URL action to QWebEngineView object when URL is changed.

2. Add actions to the navigation buttons using the build-in functions of the QWebEngineView object for reload, back, stop and forward buttons

3. Add action to the home button and inside the action change the URL to google.com or any other site.

First of all, Install PyQt5 on your IDE. Click here to get all files including images.

pip install PyQt5

Here is the code,

import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtGui import *

class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.browser = QWebEngineView()

self.browser.setUrl(QUrl('https://duckduckgo.com'))
self.setCentralWidget(self.browser)
self.showMaximized()

# navbar
navbar = QToolBar()
self.addToolBar(navbar)

# home button
home_btn = QAction(QIcon("home.png"), 'Home', self)
home_btn.triggered.connect(self.navigate_home)
navbar.addAction(home_btn)
navbar.addSeparator()

# adding back action to the tool bar
back_btn = QAction(QIcon("back.png"), 'Back', self)
back_btn.triggered.connect(self.browser.back)
navbar.addAction(back_btn)

# adding forward action to the tool bar
forward_btn = QAction(QIcon("forward.png"), 'Forward', self)
forward_btn.triggered.connect(self.browser.forward)
navbar.addAction(forward_btn)
navbar.addSeparator()

# adding reload action to the tool bar
reload_btn = QAction(QIcon("undo.png"), 'Refresh', self)
reload_btn.triggered.connect(self.browser.reload)
navbar.addAction(reload_btn)
navbar.addSeparator()
navbar.addSeparator()
self.url_bar = QLineEdit()
self.url_bar.returnPressed.connect(self.navigate_to_url)
navbar.addWidget(self.url_bar)
self.browser.urlChanged.connect(self.update_url)

# adding stop action to the tool bar
navbar.addSeparator()
stop_btn = QAction(QIcon("cross.png"), 'Remove', self)
stop_btn.setStatusTip("Stop loading current page")

# adding action to the stop button
# making browser to stop
stop_btn.triggered.connect(self.browser.stop)
navbar.addAction(stop_btn)
def navigate_home(self):
self.browser.setUrl(QUrl('https://duckduckgo.com'))

def navigate_to_url(self):
url = self.url_bar.text()
self.browser.setUrl(QUrl("https://" + url))

def update_url(self, q):
self.url_bar.setText(q.toString())


app = QApplication(sys.argv)
QApplication.setApplicationName('Rijonistic Browser')

window = MainWindow()
app.exec_()

Output:


Do you have any questions or problems about this? Please let us know in the comment section. If you want to learn programming from the internet you can read this post: Top 5 Websites to Learn Programming for Free in 2021

Post a Comment

0 Comments