Amazing-Python-Scripts
154 строки · 4.3 Кб
1# importing required libraries
2from PyQt5.QtCore import *3from PyQt5.QtWidgets import *4from PyQt5.QtGui import *5from PyQt5.QtWebEngineWidgets import *6from PyQt5.QtPrintSupport import *7import os8import sys9
10# creating main window class
11
12
13class MainWindow(QMainWindow):14
15# constructor16def __init__(self, *args, **kwargs):17super(MainWindow, self).__init__(*args, **kwargs)18
19# creating a QWebEngineView20self.browser = QWebEngineView()21
22# setting default browser url as google23self.browser.setUrl(QUrl("http://google.com"))24
25# adding action when url get changed26self.browser.urlChanged.connect(self.update_urlbar)27
28# adding action when loading is finished29self.browser.loadFinished.connect(self.update_title)30
31# set this browser as central widget or main window32self.setCentralWidget(self.browser)33
34# creating a status bar object35self.status = QStatusBar()36
37# adding status bar to the main window38self.setStatusBar(self.status)39
40# creating QToolBar for navigation41navtb = QToolBar("Navigation")42
43# adding this tool bar tot he main window44self.addToolBar(navtb)45
46# adding actions to the tool bar47# creating a action for back48back_btn = QAction("Back", self)49
50# setting status tip51back_btn.setStatusTip("Back to previous page")52
53# adding action to the back button54# making browser go back55back_btn.triggered.connect(self.browser.back)56
57# adding this action to tool bar58navtb.addAction(back_btn)59
60# similarly for forward action61next_btn = QAction("Forward", self)62next_btn.setStatusTip("Forward to next page")63
64# adding action to the next button65# making browser go forward66next_btn.triggered.connect(self.browser.forward)67navtb.addAction(next_btn)68
69# similarly for reload action70reload_btn = QAction("Reload", self)71reload_btn.setStatusTip("Reload page")72
73# adding action to the reload button74# making browser to reload75reload_btn.triggered.connect(self.browser.reload)76navtb.addAction(reload_btn)77
78# similarly for home action79home_btn = QAction("Home", self)80home_btn.setStatusTip("Go home")81home_btn.triggered.connect(self.navigate_home)82navtb.addAction(home_btn)83
84# adding a separator in the tool bar85navtb.addSeparator()86
87# creating a line edit for the url88self.urlbar = QLineEdit()89
90# adding action when return key is pressed91self.urlbar.returnPressed.connect(self.navigate_to_url)92
93# adding this to the tool bar94navtb.addWidget(self.urlbar)95
96# adding stop action to the tool bar97stop_btn = QAction("Stop", self)98stop_btn.setStatusTip("Stop loading current page")99
100# adding action to the stop button101# making browser to stop102stop_btn.triggered.connect(self.browser.stop)103navtb.addAction(stop_btn)104
105# showing all the components106self.show()107
108# method for updating the title of the window109def update_title(self):110title = self.browser.page().title()111self.setWindowTitle("% s - Epic Browser" % title)112
113# method called by the home action114def navigate_home(self):115
116# open the google117self.browser.setUrl(QUrl("http://www.google.com"))118
119# method called by the line edit when return key is pressed120def navigate_to_url(self):121
122# getting url and converting it to QUrl objetc123q = QUrl(self.urlbar.text())124
125# if url is scheme is blank126if q.scheme() == "":127# set url scheme to html128q.setScheme("http")129
130# set the url to the browser131self.browser.setUrl(q)132
133# method for updating url134# this method is called by the QWebEngineView object135def update_urlbar(self, q):136
137# setting text to the url bar138self.urlbar.setText(q.toString())139
140# setting cursor position of the url bar141self.urlbar.setCursorPosition(0)142
143
144# creating a pyQt5 application
145app = QApplication(sys.argv)146
147# setting name to the application
148app.setApplicationName("Epic Browser")149
150# creating a main window object
151window = MainWindow()152
153# loop
154app.exec_()155