分享pywebio,pyinstaller的基本用法,學會了pywebio 可以讓你快速做出各種有趣使用的小工具!
2022年來了,幾個同事讓我給他們寫個展示下python能干啥。于是我給他們寫了一個計算器,讓他們看看。
本文用到一個比較有意思的庫:pywebio。
pip install pyinstallerpip install pywebio
from pywebio import start_serverfrom pywebio.output import *from pywebio.pin import *def calc(exp): try: return f"{exp}={eval(exp)}" except: return f"{exp}:表達式不正確"def refresh(exp): out = calc(exp) with use_scope("aa", clear=True): put_text(out)def webmain(): put_markdown("## 簡易計算器 /n 輸入計算表達式:") put_input("res") put_buttons(["計算"], lambda _: refresh(pin.res))start_server(webmain, host="127.0.0.1", port=2022)
運行上述程序,在瀏覽器中輸入 http://127.0.0.1:2022/ ,得到如下效果:
pyi-makespec -F main.py #-F: 打包成一個軟件包
from pywebio.utils import pyinstaller_datas # 開始增加這一句!a = Analysis(...datas=pyinstaller_datas(), # 修改成一摸一樣...
pyinstaller main.spec
在打包路徑dist文件夾下,有一個文件:main.exe,可以直接打開,或者發(fā)給同事。
本文pywebio的大包有些技巧。大多數(shù)程序的打包可以無需配置文件,直接打包即可。使用如下命令打包:
pyinstaller -F main.py -i icon.ico
上面分享了pywebio,pyinstaller的基本用法,學會了pywebio 可以讓你快速做出各種有趣實用的小工具!
本文鏈接:http://www.www897cc.com/showinfo-26-54004-0.htmlPython 30行代碼內(nèi)制作一個計算器
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com