背景:
最近了解了一个新的打包框架BeeWare,据说他支持的平台要比nuitka更多。利用之前访问博客的脚本,来尝试一下打包windows应用程序。
BeeWare 从零开始
-
环境:
python -m pip install briefcase
-
使用pycharm新建了一个名为
beeware-venv
conda虚拟环境,一个名为pythonProject
的python项目 -
新建默认应用helloworld:
briefcase new
,一直默认回车即可 -
开发模式运行程序。进入新创建的
helloworld
目录下执行命令 :briefcase dev
(D:\work\beeware-venv) D:\Qredsun\replay_sys\demo\pythonProject\helloworld>briefcase dev [helloworld] Starting in dev mode... =========================================================================== Hello, jom Hello, jom Hello, jom Hello, jom Stopping...
-
创建
briefcase create
(D:\work\beeware-venv) D:\Qredsun\replay_sys\demo\pythonProject\helloworld>briefcase create [helloworld] Generating application template... Assigning helloworld an application GUID of 93bbf2ae-12d0-580c-aeb0-965dc4c04b69 Using app template: https://github.com/beeware/briefcase-windows-app-template.git, branch v0.3.19 Cloning template 'https://github.com/beeware/briefcase-windows-app-template.git'... Using existing template (sha 8f153808429f87d7df42baf69db1d1c60fd62a88, updated Tue Jun 11 21:41:21 2024) [helloworld] Installing support package... Using support package https://www.python.org/ftp/python/3.8.10/python-3.8.10-embed-amd64.zip Downloading python-3.8.10-embed-amd64.zip... -------------------------------------------------- 100.0% • 00:08 Unpacking support package... done [helloworld] Installing stub binary... Using stub binary https://briefcase-support.s3.amazonaws.com/python/3.8/windows/GUI-Stub-3.8-b5.zip Downloading GUI-Stub-3.8-b5.zip... -------------------------------------------------- 100.0% • 00:00 Installing stub binary... done [helloworld] Installing application code... Installing src/helloworld... done [helloworld] Installing requirements... Collecting toga-winforms~=0.4.5 Using cached toga_winforms-0.4.5-py3-none-any.whl.metadata (3.2 kB) Collecting pythonnet>=3.0.0 (from toga-winforms~=0.4.5) Using cached pythonnet-3.0.3-py3-none-any.whl.metadata (6.6 kB) Collecting toga-core==0.4.5 (from toga-winforms~=0.4.5) Using cached toga_core-0.4.5-py3-none-any.whl.metadata (5.5 kB) Collecting travertino>=0.3.0 (from toga-core==0.4.5->toga-winforms~=0.4.5) Using cached travertino-0.3.0-py3-none-any.whl.metadata (4.3 kB) Collecting importlib-metadata>=4.4.0 (from toga-core==0.4.5->toga-winforms~=0.4.5) Using cached importlib_metadata-8.0.0-py3-none-any.whl.metadata (4.6 kB) Collecting clr-loader<0.3.0,>=0.2.6 (from pythonnet>=3.0.0->toga-winforms~=0.4.5) Using cached clr_loader-0.2.6-py3-none-any.whl.metadata (1.4 kB) Collecting cffi>=1.13 (from clr-loader<0.3.0,>=0.2.6->pythonnet>=3.0.0->toga-winforms~=0.4.5) Using cached cffi-1.16.0-cp38-cp38-win_amd64.whl.metadata (1.5 kB) Collecting zipp>=0.5 (from importlib-metadata>=4.4.0->toga-core==0.4.5->toga-winforms~=0.4.5) Using cached zipp-3.19.2-py3-none-any.whl.metadata (3.6 kB) Collecting pycparser (from cffi>=1.13->clr-loader<0.3.0,>=0.2.6->pythonnet>=3.0.0->toga-winforms~=0.4.5) Using cached pycparser-2.22-py3-none-any.whl.metadata (943 bytes) Using cached toga_winforms-0.4.5-py3-none-any.whl (379 kB) Using cached toga_core-0.4.5-py3-none-any.whl (128 kB) Using cached pythonnet-3.0.3-py3-none-any.whl (290 kB) Using cached clr_loader-0.2.6-py3-none-any.whl (51 kB) Using cached importlib_metadata-8.0.0-py3-none-any.whl (24 kB) Using cached travertino-0.3.0-py3-none-any.whl (16 kB) Using cached cffi-1.16.0-cp38-cp38-win_amd64.whl (181 kB) Using cached zipp-3.19.2-py3-none-any.whl (9.0 kB) Using cached pycparser-2.22-py3-none-any.whl (117 kB) Installing collected packages: zipp, travertino, pycparser, importlib-metadata, cffi, toga-core, clr-loader, pythonnet, toga-winforms Successfully installed cffi-1.16.0 clr-loader-0.2.6 importlib-metadata-8.0.0 pycparser-2.22 pythonnet-3.0.3 toga-core-0.4.5 toga-winforms-0.4.5 travertino-0.3.0 zipp-3.19.2 Installing app requirements... done [helloworld] Installing application resources... [helloworld] Removing unneeded app content... Removing unneeded app bundle content... done [helloworld] Created build\helloworld\windows\app
-
构建
briefcase build
(D:\work\beeware-venv) D:\Qredsun\replay_sys\demo\pythonProject\helloworld>briefcase build [rcedit] RCEdit was not found; downloading and installing... Downloading rcedit-x64.exe... -------------------------------------------------- 100.0% • 00:01 [helloworld] Building App... Renaming stub binary... done Setting stub app details... done [helloworld] Built build\helloworld\windows\app\src\Hello World.exe
-
运行
briefcase run
, 添加参数-u
实时使用最新代码运行,帮助定位打包过程中的bug(D:\work\beeware-venv) D:\Qredsun\replay_sys\demo\pythonProject\helloworld>briefcase run [helloworld] Starting app... =========================================================================== Hello, a Hello, a Hello, a Hello, a Hello, a Hello, a
-
打包
briefcase package
生成目录文件:dist\Hello World-0.0.1.msi
; 添加参数--adhoc-sign
可以避免验签(D:\work\beeware-venv) D:\Qredsun\replay_sys\demo\pythonProject\helloworld>briefcase package [wix] The WiX toolset was not found; downloading and installing... Downloading wix314-binaries.zip... -------------------------------------------------- 100.0% • 00:18 Installing WiX... done ************************************************************************* ** WARNING: No signing identity provided ** ************************************************************************* Briefcase will not sign the app. To provide a signing identity, use the `--identity` option; or, to explicitly disable signing, use `--adhoc-sign`. ************************************************************************* [helloworld] Building MSI... Compiling application manifest... Compiling... done Compiling application installer... helloworld.wxs helloworld-manifest.wxs Compiling... done Linking application installer... Linking... done [helloworld] Packaged dist\Hello World-0.0.1.msi
-
安装后,可以在路径:
C:\Program Files\Jane Developer\Hello World
找到该应用
在这里插入图片描述
-
更新后,将新代码更新到老的安装包中
briefcase update
(D:\work\beeware-venv) D:\Qredsun\replay_sys\demo\pythonProject\helloworld>briefcase update [helloworld] Updating application code... Installing src/helloworld... done [helloworld] Removing unneeded app content... Removing unneeded app bundle content... done [helloworld] Application updated.
-
将跑阅读量的脚本放在app.py同级目录下,修改app.py内容如下:
""" My first application """ import toga from toga.style import Pack from toga.style.pack import COLUMN, ROW from helloworld.readCounts import run,stop def greeting(name): if name: return name else: return "qq_17328759" class HelloWorld(toga.App): def startup(self): """Construct and show the Toga application. Usually, you would add your application to a main content box. We then create a main window (with a name matching the app), and show the main window. """ main_box = toga.Box() name_label = toga.Label( "CSDN 博客账号: ", style=Pack(padding=(0, 5)), ) self.name_input = toga.TextInput(style=Pack(flex=1)) name_box = toga.Box(style=Pack(direction=ROW, padding=5)) name_box.add(name_label) name_box.add(self.name_input) self.run_button = toga.Button( "加油! 明天排名更靠前。", on_press=self.start_work, style=Pack(padding=5), ) self.down_button = toga.Button( "加油! 明天排名更靠前。", on_press=self.stop_work, style=Pack(padding=5), ) self.down_button.enabled=False main_box.add(name_box) main_box.add(self.run_button) main_box.add(self.down_button) from toga import Position from toga import Size position = Position(400,400) win_size = Size(480, 240) self.main_window = toga.MainWindow(title=self.formal_name, position=position, size=win_size) self.main_window.content = main_box self.main_window.enabled=True def start_work(self, widget): csdn_user = greeting(self.name_input.value) self.main_window.info_dialog( "Hi!", f'增加用户 {csdn_user} 博客阅读量 {self.main_window.position}', ) self.name_input.value = csdn_user self.name_input.enabled=False run(csdn_user) self.down_button.enabled=True self.run_button.enabled=False def stop_work(self, widget): '停止当前任务' self.main_window.info_dialog( "Hi!", '停止后台刷量任务', ) stop() self.down_button.enabled=False self.run_button.enabled=True self.name_input.enabled=True def main(): return HelloWorld()
-
重复更新代码、重新打包的操作,END!