A
A
Anton2018-07-24 14:43:17
Python
Anton, 2018-07-24 14:43:17

How to open different web pages using PyQT?

There is this code, it displays the html page:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import webbrowser
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
import sys
from PyQt5 import QtCore, QtWidgets

s = str("""<html><head></head><body>Привет мир!!!</body></html>""")
s2 = str("""<html><head></head><body>Прощай мир!!!</body></html>""")

def clikbtn(QWebEnginePage):
    MainWindow.__init__()
     # Что то должно произойти

class MainWindow(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.resize(700, 500)
        self.view = QWebEngineView(self)
        mypage = MyPage(self.view)
        self.view.setPage(mypage)
        mypage.setHtml(s)

        btn = QPushButton('Button', self)
        btn.setToolTip('This is a <b>QPushButton</b> widget')
        btn.resize(btn.sizeHint())
        btn.move(10, 370)

        grid = QGridLayout()
        grid.addWidget(self.view, 0, 0)
        self.setLayout(grid)
        btn.clicked.connect(clikbtn) # Кнопка обновления карты
        self.show()

class MyPage(QWebEnginePage):
    def __init__(self, parent):
        super().__init__(parent)
        self.in_window = False      # придумал переменную
    def createWindow(self, type):   # которую мы
        self.in_window = True       # тутже изменяем если просится
        return self                 # открытие в новом окне
    def acceptNavigationRequest(self, QUrl, type, isMainFrame):
        url_string = QUrl.toString()
        print(type, isMainFrame, QUrl)
        if  self.in_window and type==2 and url_string != "https://yandex.ru":
            webbrwser.open(url_string)
            self.in_window = False
            self.setHtml(s)
        return True

if __name__ == '__main__':
    app = None
    if not QApplication.instance():
        app = QApplication([])
    dlg = MainWindow()
    if app: app.exec_()

I can’t figure out how, how, by pressing the btn button, to force the html page to reload, while putting in the setHtml() variable not s, but s2 ?
Well, how and what to put, I'm still able to figure it out, but here's how to refresh the page :(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton, 2018-07-26
@8toni8

Here's what I ended up with:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import webbrowser
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
import sys
from PyQt5 import QtCore, QtWidgets
 
s = str("""<html><head></head><body>Привет мир!!!</body></html>""")
s2 = str("""<html><head></head><body>Прощай мир!!!</body></html>""")
 
onOff = True
 
def clikbtn():
    MainWindow.mypage.setHtml(s)
     # Что то должно произойти
 
class MainWindow(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.resize(700, 500)
        self.view = QWebEngineView(self)
        mypage = MyPage(self.view)
        self.view.setPage(mypage)
        #mypage.setHtml(s)
 
        combo = QComboBox(self)
        combo.addItems(['s', 's2'])
        combo.move(50, 50)
        #self.lbl.move(50, 150)
        combo.activated[str].connect(self.onActivated)
 
        btn = QPushButton('Обовить страницу', self)
        btn.setToolTip('This is a <b>QPushButton</b> widget')
        btn.resize(btn.sizeHint())
        btn.move(10, 370)
 
        grid = QGridLayout()
        grid.addWidget(self.view, 0, 0)
        self.setLayout(grid)
        btn.clicked.connect(self.clikbtn) # Кнопка обновления карты
        self.show()
 
    def clikbtn(self):
 
        self.view.reload()
 
    def onActivated(self, text):
        if text == "s":
            self.view.setHtml(s)
        elif text == "s2":
            self.view.setHtml(s2)
 
     # Что то должно произойти
 
class MyPage(QWebEnginePage):
    def __init__(self, parent):
        super().__init__(parent)
        self.in_window = False      # придумал переменную
    def createWindow(self, type):   # которую мы
        self.in_window = True       # тутже изменяем если просится
        return self                 # открытие в новом окне
    def acceptNavigationRequest(self, QUrl, type, isMainFrame):
        url_string = QUrl.toString()
        print(type, isMainFrame, QUrl)
        if  self.in_window and type==2 and url_string != "https://yandex.ru":
            webbrwser.open(url_string)
            self.in_window = False
            self.setHtml(s)
        return True
 
if __name__ == '__main__':
    app = None
    if not QApplication.instance():
        app = QApplication([])
    dlg = MainWindow()
    if app: app.exec_()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question