使用PySide2嵌套HTML使用Flask响应

1. project 结构

项目结构

  1. dist文件夹为前端内容文件夹,其中包括前端的html, css, js等内容;

  2. pyside_flask_desk文件夹内**init.py**文件为创建Qt窗口,并使用线程启动flask;

  3. run_test.py为程序主入口;

2. 程序主入口 run_test.py

#! -*- coding:utf-8 -*-
import sys
import os
from app import *
from PySide2.QtCore import QFileInfo
from pyside_flask_desk import init_gui

sys.path.append((os.path.dirname(os.path.abspath(os.path.dirname(__file__)))).replace("\\", "/"))


if __name__ == '__main__':
    root = QFileInfo(__file__).absolutePath()
    init_gui(app, port=5000, window_title="pyside2_test", icon=root + "/dist/favicon.ico")


注意:其中icon的路径必须使用绝对路径,相对路径无法加载

3. init_gui 函数创建Qt主窗口,线程启动flask

import sys
from PySide2 import QtCore, QtWidgets, QtGui, QtWebEngineWidgets
import socket


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):
        # flask app.run()
        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 init_gui(application, icon, port, width=800, height=600,
             window_title="PyFladesk", argv=None):
    if argv is None:
        argv = sys.argv

    # 创建主窗口框架
    qtapp = QtWidgets.QApplication(argv)
    
    # 线程启动flask
    webapp = ApplicationThread(application, port)
    webapp.start()
    # connect function:关闭qtapp窗口时,调用flask的终止
    qtapp.aboutToQuit.connect(webapp.terminate) 

    # 创建主窗口,并设置大小,标题,icon
    window = QtWidgets.QMainWindow()
    window.resize(width, height)
    window.setWindowTitle(window_title)
    window.setWindowIcon(QtGui.QIcon(icon))

    # 创建WebEngineView并套入主窗口
    webView = QtWebEngineWidgets.QWebEngineView(window)
    window.setCentralWidget(webView)

    # 通过home()加载url,并将page放入view
    page = WebPage('http://localhost:{}'.format(port))
    page.home()
    webView.setPage(page)
	
    # 显示窗口
    window.show()
    # 进入程序的主循环直到exit()被调用
    return qtapp.exec_()

4. app.py flask后端响应

from flask import Flask,  render_template

app = Flask(__name__, static_url_path='', static_folder='dist', template_folder='dist')


app.config['EXPLAIN_TEMPLATE_LOADING'] = True

# 路由
@app.route('/')
def greet():
    return render_template('index.html')


注意:其中static_url_path=''不可缺少,是由于前端html中将资源加载的目录写死为根目录。

static_folder='dist', template_folder='dist'根据具体project的实际情况写html, css等文件在的文件夹即可。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
使用PySide2生成可旋转的3D场景,您可以使用Qt 3D框架中的相机(QCamera)和视图(QViewport)组件。以下是一个简单的示例,演示如何使用PySide2创建可旋转的3D场景: ```python import sys from PySide2.Qt3DCore import Qt3DCore from PySide2.Qt3DExtras import Qt3DExtras from PySide2.Qt3DRender import Qt3DRender from PySide2.QtGui import QGuiApplication from PySide2.QtWidgets import QWidget, QHBoxLayout app = QGuiApplication(sys.argv) # create a window and a 3D scene widget = QWidget() layout = QHBoxLayout(widget) view = Qt3DExtras.Qt3DWindow() scene = Qt3DCore.QEntity() # create a 3D model entity and add it to the scene model = Qt3DRender.QMesh() model.setSource(QUrl.fromLocalFile("path/to/model/file")) entity = Qt3DCore.QEntity(scene) entity.addComponent(model) # create a camera and a viewport camera = Qt3DRender.QCamera(scene) camera.setViewCenter(Qt3DCore.QVector3D(0, 0, 0)) camera.setPosition(Qt3DCore.QVector3D(0, 0, 40)) view_port = Qt3DExtras.QViewport(camera) view.setRootEntity(scene) view.setViewport(view_port) # add the scene to the view and show the window layout.addWidget(view.container()) widget.show() sys.exit(app.exec_()) ``` 在代码中,我们创建了一个相机和一个视口,然后将它们添加到3D场景中。我们还设置了相机的位置和视角中心,以控制场景的旋转。最后,我们将场景添加到视图中,并显示窗口。 请注意,此示例仅演示了如何创建可旋转的3D场景。如果您需要更复杂的3D场景,您可能需要使用其他Qt 3D组件和类。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值