Build an Entire Web Browser With Python

 With the use of PyQT5 you can make applications with python. Make sure to do this in pycharm. Please install this in your project: pip install PyQt5, pip install PyQtWebEngine

 

Find the window name section in the code and rename it to whatever you want. The Code : 

import sys

from PyQt5.QtCore import QUrl,QFile, QTextStream

from PyQt5.QtWidgets import QApplication, QMainWindow, QLineEdit, QToolBar, QPushButton

from PyQt5.QtWebEngineWidgets import QWebEngineView,QWebEnginePage

 

class Browser(QMainWindow):

    def __init__(self):

        super().__init__()

        self.setWindowTitle("My Browser")

       

        self.view = QWebEngineView(self)

        self.setStyleSheet(self.read_stylesheet())

 

    def read_stylesheet(self):

        file = QFile("styles.css")

        # Create the address bar

        self.address_bar = QLineEdit()

        self.address_bar.returnPressed.connect(self.load_url)

 

        # Create the toolbar

        self.toolbar = QToolBar()

        self.toolbar.addWidget(self.address_bar)

 

        # Create back button

        self.back_button = QPushButton("<")

        self.back_button.clicked.connect(self.view.back)

        self.toolbar.addWidget(self.back_button)

 

        # Create forward button

        self.forward_button = QPushButton(">")

        self.forward_button.clicked.connect(self.view.forward)

        self.toolbar.addWidget(self.forward_button)

 

        self.addToolBar(self.toolbar)

 

        self.view.load(QUrl("http://www.google.com"))

        self.setCentralWidget(self.view)

        self.view.loadFinished.connect(self.update_back_forward_buttons)

       

    def load_url(self):

        url = self.address_bar.text()

        self.view.load(QUrl(url))

       

    def update_back_forward_buttons(self):

        self.back_button.setEnabled(self.view.page().action(QWebEnginePage.Back).isEnabled())

        self.forward_button.setEnabled(self.view.page().action(QWebEnginePage.Forward).isEnabled())

 

app = QApplication(sys.argv)

browser = Browser()

browser.show()

sys.exit(app.exec_())

 

0 comments:

Post a Comment