Flask + PyInstaller = 客户端
有些非凡状况须要开发客户端,Python有几个罕用的几个GUI框架,如 PyQt、wxPython等
但应用这些GUI框架往往界面比拟丑,而且GUI的线程问题解决起来比拟麻烦,界面主线程无奈回调,做个倒计时之类的货色都麻烦
不如间接前后端拆散,应用flask做客户端的服务,html写页面,应用pyinstaller打包成exe,这样能够在任何windows电脑点击exe关上应用
装置依赖
pip install flask pyinstaller
文件构造
root
templates
- hello.html
- application.py
代码
application.py
import webbrowserfrom flask import Flask, render_templateapp = Flask(__name__)@app.route("/")def hello_world(): return render_template('hello.html')if __name__ == '__main__': webbrowser.open('http://localhost:15000') app.run(host='localhost', port=15000)
templates/hello.html
<!doctype html><html><head> <title>演示客户端</title></head><body> <h1>演示</h1> <p>演示如何应用Flask + PyInstaller制作客户端</p></body></html>
打包
pyinstaller application.py --add-data=templates;templates --name=demo
执行后,将在我的项目目录下生产 dist/demo 目录,双击 dist/demo/demo.exe 即可关上客户端
- 也可应用python调用pyinstaller,运行上面这段代码和下面的打包命令一样的成果
from PyInstaller.__main__ import runif __name__ == '__main__': opts = ['application.py', '--add-data=templates;templates', '--name=demo'] run(opts)