Created
October 3, 2022 05:43
-
-
Save arifsuhan/584e9cbbd862391b6fa2a02fbec0b3a0 to your computer and use it in GitHub Desktop.
Create your own browser | Python | PyQt5 | QtWebEngine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from PyQt5.QtCore import * | |
from PyQt5.QtWidgets import * | |
from PyQt5.QtWebEngineWidgets import * | |
class CustomWindow(QMainWindow): | |
def __init__(self): | |
super(CustomWindow, self).__init__() | |
self.browser = QWebEngineView() | |
self.default_url = 'http://google.com' | |
self.browser.setUrl(QUrl(self.default_url)) | |
self.setCentralWidget(self.browser) | |
self.showMaximized() | |
self.navbar = "" | |
self.setup_navbar() | |
def setup_navbar(self): | |
# navbar | |
self.navbar = QToolBar() | |
self.addToolBar(self.navbar) | |
self.create_nav_btn('Back', self.browser.back) | |
self.create_nav_btn('Reload', self.browser.reload) | |
self.create_nav_btn('Forward', self.browser.forward) | |
self.create_nav_btn('Home', self.navigate_home) | |
self.url_bar = QLineEdit() | |
self.url_bar.returnPressed.connect(self.navigate_to_url) | |
self.navbar.addWidget(self.url_bar) | |
self.browser.urlChanged.connect(self.update_url) | |
def create_nav_btn(self, btn_name, action_obj): | |
temp_btn = QAction(btn_name, self) | |
temp_btn.triggered.connect(action_obj) | |
self.navbar.addAction(temp_btn) | |
def navigate_home(self): | |
self.browser.setUrl(QUrl(self.default_url)) | |
def navigate_to_url(self): | |
url = self.url_bar.text() | |
self.browser.setUrl(QUrl(url)) | |
def update_url(self, obj): | |
self.url_bar.setText(obj.toString()) | |
if __name__ == '__main__': | |
app = QApplication(sys.argv) | |
QApplication.setApplicationName("Root") | |
window = CustomWindow() | |
app.exec_() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PyQt5==5.15.7 | |
PyQt5-Qt5==5.15.2 | |
PyQt5-sip==12.11.0 | |
PyQtWebEngine==5.15.6 | |
PyQtWebEngine-Qt5==5.15.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment