You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyInstaller打包集成Flask服务的wxPython应用后,wx.html2无法正常渲染页面的问题求助

PyInstaller打包集成Flask服务的wxPython应用后,wx.html2无法正常渲染页面的问题求助

我遇到个棘手的问题:用Python解释器直接运行我的wxPython应用时一切正常,但用PyInstaller打包成exe后,wx.html2组件没法正确渲染Flask的网页内容。

我的应用逻辑是在多线程里启动Flask服务,然后用wx.html2.WebView加载本地的Flask页面。直接跑代码的时候页面能正常显示绿色背景+黄色卡片的样式,但打包成exe后就完全不对了。

以下是我的应用代码:

# webview_app.py

import wx
import wx.html2
import threading
from flask import Flask, render_template


app = Flask(__name__)


@app.route('/')
def home():
    return """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bible Display Software</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: green;
        }
        .card {
            background-color: yellow;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
            text-align: center;
        }
        h1 {
            margin: 0;
        }
    </style>
</head>
<body>
    <div class="card">
        <h1>Bible Display Software</h1>
    </div>
</body>
</html>

"""


class MainFrame(wx.Frame):
    def __init__(self):
        super().__init__(parent=None, title='WxPython Flask App', size=(800, 600))
        
        panel = wx.Panel(self)
        self.browser = wx.html2.WebView.New(panel)
        
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.browser, 1, wx.EXPAND)
        panel.SetSizer(sizer)
        self.browser.LoadURL("http://127.0.0.1:5000")


def run_flask():
    app.run(host='127.0.0.1', port=5000)


def run_wx():
    wx_app = wx.App()
    frame = MainFrame()
    frame.Show()
    wx_app.MainLoop()


if __name__ == '__main__':
    flask_thread = threading.Thread(target=run_flask, daemon=True)
    flask_thread.start()  
    run_wx()

我尝试使用的webview_app.spec配置文件如下:

#webview_app.spec
# -*- mode: python ; coding: utf-8 -*-

block_cipher = None

# Add the templates directory to the datas
added_files = [
    ('templates', 'templates'),  # (source_dir, dest_dir)
]

a = Analysis(
    ['webview_app.py'],
    pathex=[],
    binaries=[],
    datas=added_files,  # Include templates directory
    hiddenimports=['wx', 'wx.html2', 'wx._core', 'wx._html2', 'flask', 'werkzeug', 'jinja2'],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False
)

pyz = PYZ(
    a.pure,
    a.zipped_data,
    cipher=block_cipher
)

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.zipfiles,
    a.datas,
    [],
    name='WebViewApp',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=False,
    disable_windowed_traceback=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
    windowed=True
)

有没有大佬能帮我排查下问题?打包后为什么wx.html2没法正常渲染Flask的页面呢?

备注:内容来源于stack exchange,提问作者Oluwatosin Durodola

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 08:28:07