# Importing necessary classes from PyQt5 library from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import QIcon from PyQt5.QtWebEngineWidgets import * class MyWebBrowser(): # Creating a class for the web browser """ A simple web browser application using PyQt5. """ def __init__(self): """ Initialize the web browser. """ self.window = QWidget() # Creating a QWidget instance self.window.setWindowTitle("My Web Browser") # Setting the window title # main layout: holds browser and horizontal layout self.layout = QVBoxLayout() # holds the buttons and url bar self.horizontal = QHBoxLayout() self.url_bar = QLineEdit() self.url_bar.setMaximumHeight(30) self.go_btn = QPushButton("Go") self.go_btn.setMinimumHeight(30) # to load a page when enter is pressed in the url bar self.url_bar.returnPressed.connect(self.go_btn.click) # Connect the returnPressed signal to the go_btn click slot self.back_btn = QPushButton("<") self.back_btn.setMinimumHeight(30) self.forward_btn = QPushButton(">") self.forward_btn.setMinimumHeight(30) self.reload_btn = QPushButton() self.reload_btn.setIcon(QIcon('images/reload.png')) # Set the icon using the image file path self.reload_btn.setMinimumHeight(30) # Adding the widgets to the horizontal layout self.horizontal.addWidget(self.reload_btn) self.horizontal.addWidget(self.url_bar) self.horizontal.addWidget(self.go_btn) self.horizontal.addWidget(self.back_btn) self.horizontal.addWidget(self.forward_btn) self.browser = QWebEngineView() # connecting buttons self.go_btn.clicked.connect(lambda: self.navigate(self.url_bar.text())) self.back_btn.clicked.connect(self.go_back) self.forward_btn.clicked.connect(self.go_forward) self.reload_btn.clicked.connect(self.browser.reload) self.layout.addLayout(self.horizontal) # Adding the horizontal layout to the main layout self.layout.addWidget(self.browser) # Adding the browser to the main layout self.browser.setUrl(QUrl("https://google.com")) # Setting the initial URL for the browser self.window.setLayout(self.layout) # Setting the main layout for the window self.window.show() def navigate(self, url): """ Navigate to the specified URL. Args: url (str): The URL to navigate to. """ if not url.startswith("http"): # Checking if the URL does not start with "http" url = "https://" + url # Prepending "https://" to the URL self.url_bar.setText(url) # Updating the URL bar text with the modified URL self.browser.setUrl(QUrl(url)) # Navigating the browser to the specified URL def go_back(self): """ Go back to the previous page. """ self.url_bar.setText("") self.browser.back() def go_forward(self): """ Go forward to the next page. """ self.url_bar.setText("") self.browser.forward() app = QApplication([]) # Creating a QApplication instance window = MyWebBrowser() # Creating an instance of the MyWebBrowser class app.exec_() # Starting the application event loop