WXPYTHON应用程序与Pyinstaller一起编译后应该起作用,因为它应该可以使用。当我使用Python解释器运行Python代码时,它可以正常工作。但是,当它捆绑到app.exe中时,web.html2不能正确渲染烧瓶网页。 这是代码:
# 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.WebViewBackendIE
而不是
wx.html2.WebViewBackendEdge
。 原因是Pyinstaller不复制接口DLL
\Lib\site-packages\wx\WebView2Loader.dll
请参阅this wxpython票有关详细信息
我为获得边缘用作后端的EXE做了什么
rem change WINPYDIR to where your python is
rem set WINPYDIR="C:\Program Files\python"
pyinstaller ./app.py --distpath ./vdist -y --clean --contents-directory . --add-binary "%WINPYDIR%\Lib\site-packages\wx\WebView2Loader.dll:wx"
可以在.spec文件中避免绝对路径:
将以下内容添加到.spec文件的顶部
from PyInstaller import HOMEPATH
binaries=[(f’{HOMEPATH}/wx/WebView2Loader.dll’, ‘.’)],