server.py
import os
import sys
from flask import Flask, send_from_directory
if getattr(sys, "frozen", False):
base_dir = sys._MEIPASS
else:
base_dir = os.path.dirname(os.path.abspath(__file__))
app = Flask(__name__, static_folder=os.path.join(base_dir, "vue_dist"), static_url_path="/")
@app.route("/", defaults={"path": ""})
@app.route("/<path:path>")
def catch_all(path):
return app.send_static_file("index.html")
if __name__ == "__main__":
app.run(port=5000)
main.py
import os
import sys
import ctypes
import webview
import threading
from server import app
def get_resource_path(relative_path):
if getattr(sys, "frozen", False):
base_path = sys._MEIPASS
else:
base_path = os.path.dirname(os.path.abspath(__file__))
return os.path.join(base_path, relative_path)
def set_window_icon(window, icon_path):
try:
icon_handle = ctypes.windll.user32.LoadImageW(
0, icon_path, 1, 0, 0, 0x00000010
)
if not icon_handle:
raise Exception("Failed to load icon")
hwnd = webview.windows[0]._window_handle
ctypes.windll.user32.SendMessageW(hwnd, 0x0080, 0, icon_handle)
except Exception as e:
print(f"Error setting window icon: {e}")
def run_server():
app.run(port=5000)
if __name__ == "__main__":
server_thread = threading.Thread(target=run_server)
server_thread.daemon = True
server_thread.start()
icon_path = get_resource_path("vue_dist/favicon.ico")
window = webview.create_window(
"易聪云科技",
url="http://localhost:5000",
width=1024,
height=768,
resizable=True,
)
webview.start()
set_window_icon(window, icon_path)
vue_dist 是vue项目打包后的dist目录,放在跟main.py同级目录下
打包命令:pyinstaller --onefile --windowed main.py --add-data “vue_dist;vue_dist”