Amazing-Python-Scripts

Форк
0
154 строки · 4.3 Кб
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

12

13
class MainWindow(QMainWindow):
14

15
    # constructor
16
    def __init__(self, *args, **kwargs):
17
        super(MainWindow, self).__init__(*args, **kwargs)
18

19
        # creating a QWebEngineView
20
        self.browser = QWebEngineView()
21

22
        # setting default browser url as google
23
        self.browser.setUrl(QUrl("http://google.com"))
24

25
        # adding action when url get changed
26
        self.browser.urlChanged.connect(self.update_urlbar)
27

28
        # adding action when loading is finished
29
        self.browser.loadFinished.connect(self.update_title)
30

31
        # set this browser as central widget or main window
32
        self.setCentralWidget(self.browser)
33

34
        # creating a status bar object
35
        self.status = QStatusBar()
36

37
        # adding status bar to the main window
38
        self.setStatusBar(self.status)
39

40
        # creating QToolBar for navigation
41
        navtb = QToolBar("Navigation")
42

43
        # adding this tool bar tot he main window
44
        self.addToolBar(navtb)
45

46
        # adding actions to the tool bar
47
        # creating a action for back
48
        back_btn = QAction("Back", self)
49

50
        # setting status tip
51
        back_btn.setStatusTip("Back to previous page")
52

53
        # adding action to the back button
54
        # making browser go back
55
        back_btn.triggered.connect(self.browser.back)
56

57
        # adding this action to tool bar
58
        navtb.addAction(back_btn)
59

60
        # similarly for forward action
61
        next_btn = QAction("Forward", self)
62
        next_btn.setStatusTip("Forward to next page")
63

64
        # adding action to the next button
65
        # making browser go forward
66
        next_btn.triggered.connect(self.browser.forward)
67
        navtb.addAction(next_btn)
68

69
        # similarly for reload action
70
        reload_btn = QAction("Reload", self)
71
        reload_btn.setStatusTip("Reload page")
72

73
        # adding action to the reload button
74
        # making browser to reload
75
        reload_btn.triggered.connect(self.browser.reload)
76
        navtb.addAction(reload_btn)
77

78
        # similarly for home action
79
        home_btn = QAction("Home", self)
80
        home_btn.setStatusTip("Go home")
81
        home_btn.triggered.connect(self.navigate_home)
82
        navtb.addAction(home_btn)
83

84
        # adding a separator in the tool bar
85
        navtb.addSeparator()
86

87
        # creating a line edit for the url
88
        self.urlbar = QLineEdit()
89

90
        # adding action when return key is pressed
91
        self.urlbar.returnPressed.connect(self.navigate_to_url)
92

93
        # adding this to the tool bar
94
        navtb.addWidget(self.urlbar)
95

96
        # adding stop action to the tool bar
97
        stop_btn = QAction("Stop", self)
98
        stop_btn.setStatusTip("Stop loading current page")
99

100
        # adding action to the stop button
101
        # making browser to stop
102
        stop_btn.triggered.connect(self.browser.stop)
103
        navtb.addAction(stop_btn)
104

105
        # showing all the components
106
        self.show()
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
    # method called by the home action
114
    def navigate_home(self):
115

116
        # open the google
117
        self.browser.setUrl(QUrl("http://www.google.com"))
118

119
    # method called by the line edit when return key is pressed
120
    def navigate_to_url(self):
121

122
        # getting url and converting it to QUrl objetc
123
        q = QUrl(self.urlbar.text())
124

125
        # if url is scheme is blank
126
        if q.scheme() == "":
127
            # set url scheme to html
128
            q.setScheme("http")
129

130
        # set the url to the browser
131
        self.browser.setUrl(q)
132

133
    # method for updating url
134
    # this method is called by the QWebEngineView object
135
    def update_urlbar(self, q):
136

137
        # setting text to the url bar
138
        self.urlbar.setText(q.toString())
139

140
        # setting cursor position of the url bar
141
        self.urlbar.setCursorPosition(0)
142

143

144
# creating a pyQt5 application
145
app = QApplication(sys.argv)
146

147
# setting name to the application
148
app.setApplicationName("Epic Browser")
149

150
# creating a main window object
151
window = MainWindow()
152

153
# loop
154
app.exec_()
155

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.