Why does the program always fall?
-
I'm trying to figure out the browser writing on. PyQt5♪ For some reason I don't know, it always falls below the code, even though it's written without error.
Is the problem with the equipment or is there a mistake in the code?
import os import sys from PyQt5.Qt import * from PyQt5.QtWebEngineWidgets import QWebEngineView
class AboutDialog(QDialog):
def init(self, *args, **kwargs):
super(AboutDialog, self).init(*args,**kwargs)QBtn = QDialogButtonBox.Ok self.buttonbox = QDialogButtonBox(QBtn) self.buttonbox.accepted.connect(self.accept) self.buttonbox.rejected.connect(self.reject) layout = QVBoxLayout() title = QLabel("Viola Browser") font = title.font() font.setPointSize(20) title.setFont(font) layout.addWidget(title) logo = QLabel() logo.setPixmap(QPixmap(os.path.join("images", "logo.png"))) layout.addWidget(logo) layout.addWidget(QLabel("Version 0.1")) for i in range(0, layout.count()): layout.itemAt(i).setAlignment(Qt.AlignHCenter) layout.addWidget(self.buttonbox) self.setLayout(layout)
class MainWindow(QMainWindow):
def init(self, *args, **kwargs):
super(MainWindow,self).init(*args,**kwargs)self.browser = QWebEngineView() self.browser.setUrl(QUrl("https://google.com")) self.browser.urlChanged.connect(self.update_urlbar) self.browser.loadFinished.connect(self.update_title) self.setCentralWidget(self.browser) self.status = QStatusBar() self.setStatusBar(self.status) navtb = QToolBar("Navigation") navtb.setIconSize(QSize(16,16)) self.addToolBar(navtb) back_btn = QAction(QIcon(os.path.join("images","arrow-180.png")), "Back", self) back_btn.setStatusTip("Back to previous page") back_btn.triggered.connect(self.browser.back) navtb.addAction(back_btn) next_btn = QAction(QIcon(os.path.join("images","arrow-000.png")), "Forward", self) next_btn.setStatusTip("Forward to previous page") next_btn.triggered.connect(self.browser.forward) navtb.addAction(next_btn) reload_btn = QAction(QIcon(os.path.join("images", "arrow-circle-315.png")), "Reload", self) reload_btn.setStatusTip("Reload page") reload_btn.triggered.connect(self.browser.reload) navtb.addAction(reload_btn) home_btn = QAction(QIcon(os.path.join("images", "home.png")), "Home", self) home_btn.setStatusTip("Go home") home_btn.triggered.connect(self.navigate_home) navtb.addAction(home_btn) navtb.addSeparator() self.httpsicon = QLabel() self.httpsicon.setPixmap(QPixmap(os.path.join("images", "lock-noss1.png"))) navtb.addWidget(self.httpsicon) self.urlbar = QLineEdit() self.urlbar.returnPressed.connect(self.navigate_to_url) navtb.addWidget(self.urlbar) stop_btn = QAction(QIcon(os.path.join("images", "cross-circle.png")), "Stop", self) stop_btn.setStatusTip("Stop loading current page") stop_btn.triggered.connect(self.browser.stop) navtb.addAction(stop_btn) file_menu = self.menuBar().addMenu("&File") open_file_action = QAction(QIcon(os.path.join("images", "disk--arrow.png")), "Open file", self) open_file_action.setStatusTip("Open from file") open_file_action.triggered.connect(self.open_file) file_menu.addAction(open_file_action) help_menu = self.menuBar().addMenu("&Help") about_action = QAction(QIcon(os.path.join("images", "question.png")), "About browser", self) about_action.setStatusTip("Find out more about browser") about_action.triggered.connect(self.about) help_menu.addAction(about_action) def update_title(self): title = self.browser.page().title() self.setWindowTitle("%s - Viola" % title) def about(self): pass def open_file(self): filename, _ = QFileDialog.getOpenFileName(self,"Open file", "", "*.htm *.html" "All files (*.*)") if filename: with open(filename, "r") as f: html = f.read() self.browser.setHtml(html) self.urlbar.setText(filename) def save_file(self): filename, _ = QFileDialog.getSaveFileName(self,"save page as", "", "*.htm *.html" "All files (*.*)") if filename: html = self.browser.page().toHtml() with open(filename, "w") as f: f.write(html) def navigate_home(self): self.browser.setUrl(QUrl("https://google.com")) def navigate_to_url(self): q = QUrl(self.urlbar.text()) if q.scheme() == "": q.setSheme("http") self.browser.setUrl(q) def update_urlbar(self, q ): if q.sheme() == "https": self.httpsicon.setPixmap(QPixmap(os.path.join("images", "lock-ss1.png"))) else: self.httpsicon.setPixmap(QPixmap(os.path.join("images", "lock-noss1.png"))) self.urlbar.setText(q.toString()) self.urlbar.setCursorPosition(0)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
-
I didn't check everything, but two spots were for your attention.
import os import sys from PyQt5.Qt import * from PyQt5.QtWebEngineWidgets import QWebEngineView
class AboutDialog(QDialog):
def init(self, *args, **kwargs):
super(AboutDialog, self).init(*args,**kwargs)QBtn = QDialogButtonBox.Ok self.buttonbox = QDialogButtonBox(QBtn) self.buttonbox.accepted.connect(self.accept) self.buttonbox.rejected.connect(self.reject) layout = QVBoxLayout() title = QLabel("Viola Browser") font = title.font() font.setPointSize(20) title.setFont(font) layout.addWidget(title) logo = QLabel() logo.setPixmap(QPixmap(os.path.join("images", "logo.png"))) layout.addWidget(logo) layout.addWidget(QLabel("Version 0.1")) for i in range(0, layout.count()): layout.itemAt(i).setAlignment(Qt.AlignHCenter) layout.addWidget(self.buttonbox) self.setLayout(layout)
class MainWindow(QMainWindow):
def init(self, *args, **kwargs):
super(MainWindow,self).init(*args,**kwargs)self.browser = QWebEngineView() self.browser.setUrl(QUrl("https://google.com")) self.browser.urlChanged.connect(self.update_urlbar) self.browser.loadFinished.connect(self.update_title) self.setCentralWidget(self.browser) self.status = QStatusBar() self.setStatusBar(self.status) navtb = QToolBar("Navigation") navtb.setIconSize(QSize(16,16)) self.addToolBar(navtb) back_btn = QAction(QIcon(os.path.join("images","arrow-180.png")), "Back", self) back_btn.setStatusTip("Back to previous page") back_btn.triggered.connect(self.browser.back) navtb.addAction(back_btn) next_btn = QAction(QIcon(os.path.join("images","arrow-000.png")), "Forward", self) next_btn.setStatusTip("Forward to previous page") next_btn.triggered.connect(self.browser.forward) navtb.addAction(next_btn) reload_btn = QAction(QIcon(os.path.join("images", "arrow-circle-315.png")), "Reload", self) reload_btn.setStatusTip("Reload page") reload_btn.triggered.connect(self.browser.reload) navtb.addAction(reload_btn) home_btn = QAction(QIcon(os.path.join("images", "home.png")), "Home", self) home_btn.setStatusTip("Go home") home_btn.triggered.connect(self.navigate_home) navtb.addAction(home_btn) navtb.addSeparator() self.httpsicon = QLabel() self.httpsicon.setPixmap(QPixmap(os.path.join("images", "lock-noss1.png"))) navtb.addWidget(self.httpsicon) self.urlbar = QLineEdit() self.urlbar.returnPressed.connect(self.navigate_to_url) navtb.addWidget(self.urlbar) stop_btn = QAction(QIcon(os.path.join("images", "cross-circle.png")), "Stop", self) stop_btn.setStatusTip("Stop loading current page") stop_btn.triggered.connect(self.browser.stop) navtb.addAction(stop_btn) file_menu = self.menuBar().addMenu("&File") open_file_action = QAction(QIcon(os.path.join("images", "disk--arrow.png")), "Open file", self) open_file_action.setStatusTip("Open from file") open_file_action.triggered.connect(self.open_file) file_menu.addAction(open_file_action) help_menu = self.menuBar().addMenu("&Help") about_action = QAction(QIcon(os.path.join("images", "question.png")), "About browser", self) about_action.setStatusTip("Find out more about browser") about_action.triggered.connect(self.about) help_menu.addAction(about_action) def update_title(self): title = self.browser.page().title() self.setWindowTitle("%s - Viola" % title) def about(self): pass def open_file(self):
!!!
filename, _ = QFileDialog.getOpenFileName( self, "Open file", "images",
"*.htm *.html"
"All files (.)"
'HTML Files (*.html);;HTM Files (*.HTM)' # +++ ) if filename: with open(filename, "r") as f: html = f.read() self.browser.setHtml(html) self.urlbar.setText(filename) def save_file(self): filename, _ = QFileDialog.getSaveFileName(self,"save page as", "", "*.htm *.html" "All files (*.*)") if filename: html = self.browser.page().toHtml() with open(filename, "w") as f: f.write(html) def navigate_home(self): self.browser.setUrl(QUrl("https://google.com")) def navigate_to_url(self): q = QUrl(self.urlbar.text()) if q.scheme() == "": q.setSheme("http") self.browser.setUrl(q) def update_urlbar(self, q ): print(f'q = {q}') # QtCore.QUrl('https://www.google.com/') print(f'q = {q.path()}')
!!! scheme
if q.sheme() == "https":
if q.scheme() == "https": # +++
+++ ^^^^^^ установите свои изображения vvvvvvv
self.httpsicon.setPixmap(QPixmap(os.path.join("images", "cat.png"))) else: self.httpsicon.setPixmap(QPixmap(os.path.join("images", "Blue.png"))) self.urlbar.setText(q.toString()) self.urlbar.setCursorPosition(0)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())