how to access attribute QtWebEngine in other class

20 views Asked by At

My first file contain one class. It launch my web application with Flask : (main.py) Function init_gui open web application through QtwebEngine. QtWebengine pack into attribute webView. It work but I want to access webView attribute of init.py in my class Routeur especially in my function problem_here(self) where I would like execute webView.reload()

import pyautogui as pag
from Url import *

class Routeur(Url):
    def __init__(self, window_title):
        self.app = Flask(__name__)
        self.app.config['SECRET_KEY']= 'xxxxxxxxxxxxxxxxxxxxxx' 
        self.app.config.update(SESSION_COOKIE_HTTPONLY=True)
        self.app.config.update(SESSION_COOKIE_SECURE=True) 
        self.app.config.update(SESSION_COOKIE_SAMESITE='Strict')
        self.Url_addRoute()

    def home(self):
        return render_template('index.html', test="test")

    def problem_here(self):
        self.webView.reload()

if __name__ == '__main__':
    window_title = "test"
    routeur = Routeur(window_title)
    hauteur = int(pag.size().height - (pag.size().height * 0.118)) 
    position = int(pag.size().width - 350) 
    init_gui(routeur.app, port=2000, width=350, height=hauteur, window_title=window_title, icon="icon.png", x=position-10, y=10)


I launch class in order to execute my web application.It work. But I would like to modify some attribute of webView which is created by second file : (_init.py) :


import sys
from PySide2 import QtCore, QtWidgets, QtGui, QtWebEngineWidgets
import socket
import pyautogui as pag


class ApplicationThread(QtCore.QThread):
    def __init__(self, application, port=5000):
        super(ApplicationThread, self).__init__()
        self.application = application
        self.port = port

    def __del__(self):
        self.wait()

    def run(self):
        self.application.run(port=self.port, threaded=True)


class WebPage(QtWebEngineWidgets.QWebEnginePage):
    def __init__(self, root_url):
        super(WebPage, self).__init__()
        self.root_url = root_url

    def home(self):
        self.load(QtCore.QUrl(self.root_url))

    def acceptNavigationRequest(self, url, kind, is_main_frame):
        """Open external links in browser and internal links in the webview"""
        ready_url = url.toEncoded().data().decode()
        is_clicked = kind == self.NavigationTypeLinkClicked
        if is_clicked and self.root_url not in ready_url:
            QtGui.QDesktopServices.openUrl(url)
            return False
        return super(WebPage, self).acceptNavigationRequest(url, kind, is_main_frame)


def init_gui(application, port=0, width=800, height=600, window_title="", icon="appicon.png", argv=None):
    if argv is None:
        argv = sys.argv

    if port == 0:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.bind(('localhost', 0))
        port = sock.getsockname()[1]
        sock.close()

    # Application Level
    qtapp = QtWidgets.QApplication(argv)
    webapp = ApplicationThread(application, port)
    webapp.start()
    qtapp.aboutToQuit.connect(webapp.terminate)

    # Main Window Level
    window = QtWidgets.QMainWindow()
    window.resize(width, height)
    window.setWindowTitle(window_title)
    window.setWindowIcon(QtGui.QIcon(icon))

    # WebView Level
    webView = QtWebEngineWidgets.QWebEngineView(window)
    window.setCentralWidget(webView)

    # WebPage Level
    page = WebPage('http://localhost:{}'.format(port))
    page.home()
    webView.setPage(page)

    window.show()
    return qtapp.exec_()

My question is how to access webView in my class Routeur ??

I want to access webView in my class Routeur

0

There are 0 answers