|
| 1 | +# importing required libraries |
| 2 | +from PyQt5.QtCore import * |
| 3 | +from PyQt5.QtWidgets import * |
| 4 | +from PyQt5.QtGui import * |
| 5 | +from PyQt5.QtWebEngineWidgets import * |
| 6 | +from PyQt5.QtPrintSupport import * |
| 7 | +import os |
| 8 | +import sys |
| 9 | + |
| 10 | +# creating main window class |
| 11 | +class MainWindow(QMainWindow): |
| 12 | + |
| 13 | + # constructor |
| 14 | + def __init__(self, *args, **kwargs): |
| 15 | + super(MainWindow, self).__init__(*args, **kwargs) |
| 16 | + |
| 17 | + |
| 18 | + # creating a QWebEngineView |
| 19 | + self.browser = QWebEngineView() |
| 20 | + |
| 21 | + # setting default browser url as google |
| 22 | + self.browser.setUrl(QUrl("http://google.com")) |
| 23 | + |
| 24 | + # adding action when url get changed |
| 25 | + self.browser.urlChanged.connect(self.update_urlbar) |
| 26 | + |
| 27 | + # adding action when loading is finished |
| 28 | + self.browser.loadFinished.connect(self.update_title) |
| 29 | + |
| 30 | + # set this browser as central widget or main window |
| 31 | + self.setCentralWidget(self.browser) |
| 32 | + |
| 33 | + # creating a status bar object |
| 34 | + self.status = QStatusBar() |
| 35 | + |
| 36 | + # adding status bar to the main window |
| 37 | + self.setStatusBar(self.status) |
| 38 | + |
| 39 | + # creating QToolBar for navigation |
| 40 | + navtb = QToolBar("Navigation") |
| 41 | + |
| 42 | + # adding this tool bar tot he main window |
| 43 | + self.addToolBar(navtb) |
| 44 | + |
| 45 | + # adding actions to the tool bar |
| 46 | + # creating a action for back |
| 47 | + back_btn = QAction("Back", self) |
| 48 | + |
| 49 | + # setting status tip |
| 50 | + back_btn.setStatusTip("Back to previous page") |
| 51 | + |
| 52 | + # adding action to the back button |
| 53 | + # making browser go back |
| 54 | + back_btn.triggered.connect(self.browser.back) |
| 55 | + |
| 56 | + # adding this action to tool bar |
| 57 | + navtb.addAction(back_btn) |
| 58 | + |
| 59 | + # similarly for forward action |
| 60 | + next_btn = QAction("Forward", self) |
| 61 | + next_btn.setStatusTip("Forward to next page") |
| 62 | + |
| 63 | + # adding action to the next button |
| 64 | + # making browser go forward |
| 65 | + next_btn.triggered.connect(self.browser.forward) |
| 66 | + navtb.addAction(next_btn) |
| 67 | + |
| 68 | + # similarly for reload action |
| 69 | + reload_btn = QAction("Reload", self) |
| 70 | + reload_btn.setStatusTip("Reload page") |
| 71 | + |
| 72 | + # adding action to the reload button |
| 73 | + # making browser to reload |
| 74 | + reload_btn.triggered.connect(self.browser.reload) |
| 75 | + navtb.addAction(reload_btn) |
| 76 | + |
| 77 | + # similarly for home action |
| 78 | + home_btn = QAction("Home", self) |
| 79 | + home_btn.setStatusTip("Go home") |
| 80 | + home_btn.triggered.connect(self.navigate_home) |
| 81 | + navtb.addAction(home_btn) |
| 82 | + |
| 83 | + # adding a separator in the tool bar |
| 84 | + navtb.addSeparator() |
| 85 | + |
| 86 | + # creating a line edit for the url |
| 87 | + self.urlbar = QLineEdit() |
| 88 | + |
| 89 | + # adding action when return key is pressed |
| 90 | + self.urlbar.returnPressed.connect(self.navigate_to_url) |
| 91 | + |
| 92 | + # adding this to the tool bar |
| 93 | + navtb.addWidget(self.urlbar) |
| 94 | + |
| 95 | + # adding stop action to the tool bar |
| 96 | + stop_btn = QAction("Stop", self) |
| 97 | + stop_btn.setStatusTip("Stop loading current page") |
| 98 | + |
| 99 | + # adding action to the stop button |
| 100 | + # making browser to stop |
| 101 | + stop_btn.triggered.connect(self.browser.stop) |
| 102 | + navtb.addAction(stop_btn) |
| 103 | + |
| 104 | + # showing all the components |
| 105 | + self.show() |
| 106 | + |
| 107 | + |
| 108 | + # method for updating the title of the window |
| 109 | + def update_title(self): |
| 110 | + title = self.browser.page().title() |
| 111 | + self.setWindowTitle("% s - Epic Browser" % title) |
| 112 | + |
| 113 | + |
| 114 | + # method called by the home action |
| 115 | + def navigate_home(self): |
| 116 | + |
| 117 | + # open the google |
| 118 | + self.browser.setUrl(QUrl("http://www.google.com")) |
| 119 | + |
| 120 | + # method called by the line edit when return key is pressed |
| 121 | + def navigate_to_url(self): |
| 122 | + |
| 123 | + # getting url and converting it to QUrl objetc |
| 124 | + q = QUrl(self.urlbar.text()) |
| 125 | + |
| 126 | + # if url is scheme is blank |
| 127 | + if q.scheme() == "": |
| 128 | + # set url scheme to html |
| 129 | + q.setScheme("http") |
| 130 | + |
| 131 | + # set the url to the browser |
| 132 | + self.browser.setUrl(q) |
| 133 | + |
| 134 | + # method for updating url |
| 135 | + # this method is called by the QWebEngineView object |
| 136 | + def update_urlbar(self, q): |
| 137 | + |
| 138 | + # setting text to the url bar |
| 139 | + self.urlbar.setText(q.toString()) |
| 140 | + |
| 141 | + # setting cursor position of the url bar |
| 142 | + self.urlbar.setCursorPosition(0) |
| 143 | + |
| 144 | + |
| 145 | +# creating a pyQt5 application |
| 146 | +app = QApplication(sys.argv) |
| 147 | + |
| 148 | +# setting name to the application |
| 149 | +app.setApplicationName("Epic Browser") |
| 150 | + |
| 151 | +# creating a main window object |
| 152 | +window = MainWindow() |
| 153 | + |
| 154 | +# loop |
| 155 | +app.exec_() |
0 commit comments