Say Goodbye to Chrome: Build Your Own Browser with PyQt5 and Python

ashishpandey

Pandeyashish17

Posted on January 10, 2023

Say Goodbye to Chrome: Build Your Own Browser with PyQt5 and Python

Alright folks! Let's build a browser so unique and wild that even Chrome would be envious! We'll be using Python and PyQt5 to build this browser, so make sure you have both installed on your computer. If not, you can use the following command to install them:



pip install pyqt5
pip install PyQtWebEngine


Enter fullscreen mode Exit fullscreen mode

Now that we have the necessary tools, let's get into the code. The full code for this project can be found on this GitHub repository: https://github.com/Pandeyashish17/browser-using-python. Not only the code but also all the button images that we will be using in this project are available there.

We're using PyQt5 to create a QTabWidget and a QMainWindow, which will serve as the foundation for our browser. We're also using PyQtWebEngine to create a QWebEngineView, which will be used to display web pages.

We're creating a bunch of buttons with images like 'back.svg', 'forward.svg', 'reload.svg', 'home.svg', 'add.svg' which are available at the repository mentioned above. These buttons will have different functionality like back, forward, reload and navigate home.

We're also creating a search bar, which uses Google to search for whatever you type in. So, whether you're looking for funny cat videos or researching the latest and greatest Python libraries, our browser's got you covered!

Full Code



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

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

        # create a QTabWidget
        self.tabs = QTabWidget()
        self.setCentralWidget(self.tabs)
        self.showMaximized()

        self.home_url = "https://github.com/Pandeyashish17/browser-using-python"

        # Create a new tab and set it as the current tab
        self.add_new_tab(self.home_url, "Home")

        # create a navigation toolbar
        navbar = QToolBar()
        self.addToolBar(navbar)

        back_btn = QAction(QIcon('back.svg'),'', self)
        back_btn.triggered.connect(self.tabs.currentWidget().back)
        navbar.addAction(back_btn)

        forward_btn = QAction(QIcon('forward.svg'),'', self)
        forward_btn.triggered.connect(self.tabs.currentWidget().forward)
        navbar.addAction(forward_btn)

        reload_btn = QAction(QIcon('reload.svg'),'', self)
        reload_btn.triggered.connect(self.tabs.currentWidget().reload)
        navbar.addAction(reload_btn)

        home_btn = QAction(QIcon('home.svg'),'', self)
        home_btn.triggered.connect(self.navigate_home)
        navbar.addAction(home_btn)

        new_tab_btn = QAction(QIcon('add.svg'),'', self)
        new_tab_btn.triggered.connect(self.add_new_tab)
        navbar.addAction(new_tab_btn)

        # create a search bar
        self.search_bar = QLineEdit()
        self.search_bar.returnPressed.connect(self.search)
        navbar.addWidget(self.search_bar)


    def add_new_tab(self, url=None, title="blank"):
        browser = QWebEngineView()

        # set url if given
        if url:
            browser.setUrl(QUrl(url))

        # create a new tab and set browser as its widget
        tab_index = self.tabs.addTab(browser, title)

        # make the new tab the current tab
        self.tabs.setCurrentIndex(tab_index)

        # update the url bar
        browser.urlChanged.connect(self.update_url)

    def search(self):
        url = self.search_bar.text()
        current_tab = self.tabs.currentWidget()
        current_tab.setUrl(QUrl(f"https://google.com/search?q={url}"))

    def navigate_home(self):
        self.tabs.currentWidget().setUrl(QUrl(self.home_url))

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




app = QApplication(sys.argv)
QApplication.setApplicationName('The Silly Surfer ')
window = MainWindow()
app.exec_()




Enter fullscreen mode Exit fullscreen mode

Now, let's run the code and see our wild and unique browser in action! You can easily customize the browser as per your need and create something even more unique and wild.

Web Browser Using Python and PyQt5

And there you have it, folks! Your very own, one-of-a-kind browser built with PyQt5 and Python. Now you can surf the web in style, with all the fun buttons and icons to make your browsing experience extra delightful. Happy coding!

💖 💪 🙅 🚩
ashishpandey
Pandeyashish17

Posted on January 10, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related