Search code examples
pythonflaskpyqtpyqt5

PyQt with Flask dont show the MainWindow


i'm trayin to create a gui that run a flask application using this example https://github.com/onur2677/PyQt-Flask

in this exemple they use pyqt 4

i'm trying to use pyqt 5 so i made some changes

but the mainwindow open and close in the same time

this is my code for the gui

import sys

from PyQt5 import *
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import psycopg2
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5 import QtCore
from PyQt5 import *

from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication, QMainWindow

from PyQt5.QtCore import QEventLoop
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication

PORT = 5000
ROOT_URL = 'http://localhost:{}'.format(PORT)

class FlaskThread(QThread):

    def __init__(self, application):
        QThread.__init__(self)
        self.application = application

    def __del__(self):
        self.wait()

    def run(self):
        self.application.run(port=PORT)

def createGuiFor(application):
    qtapp = QApplication(sys.argv)
    webapp = FlaskThread(application)
    webapp.start()
    qtapp.aboutToQuit.connect(webapp.terminate)
    webview = QWebEngineView()
    webview.load(QUrl(ROOT_URL))
    webview.show()
    webview.setWindowTitle("MyApp")
    webview.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
    return qtapp.exec_()

if __name__ == '__main__':
    from MyWebApp import app
    app = QApplication(sys.argv)
    sys.exit(app.exec_())
    sys.exit(createGuiFor(app))

and this is the flask application that will run in the gui

from flask import Flask ,render_template
app = Flask(__name__)

@app.route("/")
def hello():
    return render_template("index.html")

if __name__ == "__main__":
    app.run()

Solution

  • for any one ho want to use flask and pyqt there is an example

    here https://github.com/smoqadam/PyFladesk

    that work for me perfectly

    EDIT:

    Here is the same code with PySide6

    https://github.com/eliaweiss/PySide6Fladesk.git