|
| 1 | +import sys.argv as argv |
| 2 | +from PyQt5.QtCore import QUrl |
| 3 | +from PyQt5.QtWebEngineWidgets import QWebEngineView |
| 4 | + |
| 5 | +from PyQt5.QtWidgets import * |
| 6 | +class Window(QMainWindow): |
| 7 | + def __init__(self, *args, **kwargs): |
| 8 | + super(Window, self).__init__(*args, **kwargs) |
| 9 | + |
| 10 | + self.browser = QWebEngineView() |
| 11 | + self.browser.setUrl(QUrl('https://www.google.com')) |
| 12 | + self.browser.urlChanged.connect(self.update_AddressBar) |
| 13 | + self.setCentralWidget(self.browser) |
| 14 | + self.status_bar = QStatusBar() |
| 15 | + self.setStatusBar(self.status_bar) |
| 16 | + self.navigation_bar = QToolBar('Navigation Toolbar') |
| 17 | + self.addToolBar(self.navigation_bar) |
| 18 | + back_button = QAction("Back", self) |
| 19 | + back_button.setStatusTip('Go to previous page you visited') |
| 20 | + back_button.triggered.connect(self.browser.back) |
| 21 | + self.navigation_bar.addAction(back_button) |
| 22 | + refresh_button = QAction("Refresh", self) |
| 23 | + refresh_button.setStatusTip('Refresh this page') |
| 24 | + refresh_button.triggered.connect(self.browser.reload) |
| 25 | + self.navigation_bar.addAction(refresh_button) |
| 26 | + next_button = QAction("Next", self) |
| 27 | + next_button.setStatusTip('Go to next page') |
| 28 | + next_button.triggered.connect(self.browser.forward) |
| 29 | + self.navigation_bar.addAction(next_button) |
| 30 | + home_button = QAction("Home", self) |
| 31 | + home_button.setStatusTip('Go to home page (Google page)') |
| 32 | + home_button.triggered.connect(self.go_to_home) |
| 33 | + self.navigation_bar.addAction(home_button) |
| 34 | + self.navigation_bar.addSeparator() |
| 35 | + self.URLBar = QLineEdit() |
| 36 | + self.URLBar.returnPressed.connect(lambda: self.go_to_URL(QUrl(self.URLBar.text()))) |
| 37 | + self.navigation_bar.addWidget(self.URLBar) |
| 38 | + self.addToolBarBreak() |
| 39 | + |
| 40 | + bookmarks_toolbar = QToolBar('Bookmarks', self) |
| 41 | + self.addToolBar(bookmarks_toolbar) |
| 42 | + pythongeeks = QAction("PythonGeeks", self) |
| 43 | + pythongeeks.setStatusTip("Go to PythonGeeks website") |
| 44 | + pythongeeks.triggered.connect(lambda: self.go_to_URL(QUrl("https://pythongeeks.org"))) |
| 45 | + bookmarks_toolbar.addAction(pythongeeks) |
| 46 | + facebook = QAction("Facebook", self) |
| 47 | + facebook.setStatusTip("Go to Facebook") |
| 48 | + facebook.triggered.connect(lambda: self.go_to_URL(QUrl("https://www.facebook.com"))) |
| 49 | + bookmarks_toolbar.addAction(facebook) |
| 50 | + linkedin = QAction("LinkedIn", self) |
| 51 | + linkedin.setStatusTip("Go to LinkedIn") |
| 52 | + linkedin.triggered.connect(lambda: self.go_to_URL(QUrl("https://in.linkedin.com"))) |
| 53 | + bookmarks_toolbar.addAction(linkedin) |
| 54 | + instagram = QAction("Instagram", self) |
| 55 | + instagram.setStatusTip("Go to Instagram") |
| 56 | + instagram.triggered.connect(lambda: self.go_to_URL(QUrl("https://www.instagram.com"))) |
| 57 | + bookmarks_toolbar.addAction(instagram) |
| 58 | + twitter = QAction("Twitter", self) |
| 59 | + twitter.setStatusTip('Go to Twitter') |
| 60 | + twitter.triggered.connect(lambda: self.go_to_URL(QUrl("https://www.twitter.com"))) |
| 61 | + bookmarks_toolbar.addAction(twitter) |
| 62 | + self.show() |
| 63 | + def go_to_home(self): |
| 64 | + self.browser.setUrl(QUrl('https://www.google.com/')) |
| 65 | + def go_to_URL(self, url: QUrl): |
| 66 | + if url.scheme() == '': |
| 67 | + url.setScheme('https://') |
| 68 | + self.browser.setUrl(url) |
| 69 | + self.update_AddressBar(url) |
| 70 | + def update_AddressBar(self, url): |
| 71 | + self.URLBar.setText(url.toString()) |
| 72 | + self.URLBar.setCursorPosition(0) |
| 73 | +app = QApplication(argv) |
| 74 | +app.setApplicationName('Web Browser') |
| 75 | +window = Window() |
| 76 | +app.exec_() |
0 commit comments