教程 4 - 更新应用程序¶
在上一教程中,我们将应用程序打包为本地应用程序。如果您面对的是一个真实世界的应用程序,这并不是故事的结束–您可能会进行一些测试,发现一些问题,并需要进行一些修改。即使您的应用程序是完美的,您最终还是希望发布改进后的第二版应用程序。
那么,在更改代码时如何更新已安装的应用程序?
更新应用程序代码¶
目前,我们的应用程序会在您按下按钮时打印到控制台。但是,图形用户界面应用程序实际上不应该使用控制台进行输出。它们需要使用对话框与用户交流。
让我们添加一个对话框来说 “你好”,而不是写入控制台。修改 say_hello
回调,使它看起来像这样::
def say_hello(self, widget):
self.main_window.info_dialog(
f"Hello, {self.name_input.value}",
"Hi there!",
)
这将指示 Toga 在按下按钮时打开一个模态对话框。
如果运行 briefcase dev
,输入名称并按下按钮,就会看到新的对话框:
但是,如果运行``briefcase run``,对话框就不会出现。
为什么会这样?简易开发包 “通过就地运行您的代码来运行–它试图为您的代码提供尽可能逼真的运行环境,但并不提供或使用任何平台基础架构来将您的代码包装成应用程序。打包应用程序的部分过程包括将代码复制到应用程序捆绑包中,目前,您的应用程序中仍有旧代码。
因此,我们需要告诉公文包更新应用程序,复制新版本的代码。我们可以删除旧的平台目录,然后从头开始。不过,Briefcase 提供了一种更简单的方法–您可以更新现有捆绑应用程序的代码:
(beeware-venv) $ briefcase update
[helloworld] Updating application code...
Installing src/helloworld... done
[helloworld] Removing unneeded app content...
Removing unneeded app bundle content... done
[helloworld] Application updated.
(beeware-venv) $ briefcase update
[helloworld] Finalizing application configuration...
Targeting ubuntu:jammy (Vendor base debian)
Determining glibc version... done
Targeting glibc 2.35
Targeting Python3.10
[helloworld] Updating application code...
Installing src/helloworld... done
[helloworld] Removing unneeded app content...
Removing unneeded app bundle content... done
[helloworld] Application updated.
(beeware-venv) C:\...>briefcase update
[helloworld] Updating application code...
Installing src/helloworld... done
[helloworld] Removing unneeded app content...
Removing unneeded app bundle content... done
[helloworld] Application updated.
如果 Briefcase 找不到脚手架模板,它会自动调用 create
生成一个新的脚手架。
更新了安装程序代码后,我们就可以运行 briefcase build
来重新编译应用程序,运行 briefcase run
来运行更新后的应用程序,并运行 briefcase package
来重新打包应用程序以便分发。
(macOS 用户请记住,正如 Tutorial 3 中所述,我们建议在运行 briefcase package
时使用 --adhoc-sign
标志,以避免设置代码签名身份的复杂性,并使教程尽可能简单)。
更新和运行一步到位¶
如果要快速迭代代码更改,很可能需要更改代码、更新应用程序并立即重新运行应用程序。对于大多数情况来说,开发人员模式 (briefcase dev
) 是进行这种快速迭代的最简单方法;但是,如果您要测试应用程序作为本地二进制文件运行的方式,或查找一个只有在应用程序打包时才会出现的错误,您可能需要反复调用 briefcase run
。为了简化更新和运行打包应用程序的过程,Briefcase 提供了支持这种使用模式的快捷方式 - run``命令上的 ``-u``(或 ``--update
)选项。
让我们尝试做另一个改动。您可能已经注意到,如果您不在文本输入框中输入姓名,对话框就会显示 “Hello, “。 让我们再次修改 say_hello
函数,以处理这种边缘情况。
在文件顶部,在导入和 “类 HelloWorld “定义之间,添加一个实用程序方法,以根据所提供的名称值生成适当的问候语::
def greeting(name):
if name:
return f"Hello, {name}"
else:
return "Hello, stranger"
然后,修改 say_hello
回调,以使用这个新的实用程序方法::
def say_hello(self, widget):
self.main_window.info_dialog(
greeting(self.name_input.value),
"Hi there!",
)
在开发模式下运行应用程序(使用 briefcase dev
),以确认新逻辑是否有效;然后使用一条命令更新、构建和运行应用程序:
(beeware-venv) $ briefcase run -u
[helloworld] Updating application code...
Installing src/helloworld... done
[helloworld] Removing unneeded app content...
Removing unneeded app bundle content... done
[helloworld] Application updated.
[helloworld] Building application...
...
[helloworld] Built build/helloworld/macos/app/Hello World.app
[helloworld] Starting app...
(beeware-venv) $ briefcase run -u
[helloworld] Finalizing application configuration...
Targeting ubuntu:jammy (Vendor base debian)
Determining glibc version... done
Targeting glibc 2.35
Targeting Python3.10
[helloworld] Updating application code...
Installing src/helloworld... done
[helloworld] Removing unneeded app content...
Removing unneeded app bundle content... done
[helloworld] Application updated.
[helloworld] Building application...
...
[helloworld] Built build/helloworld/linux/ubuntu/jammy/helloworld-0.0.1/usr/bin/helloworld
[helloworld] Starting app...
(beeware-venv) C:\...>briefcase run -u
[helloworld] Updating application code...
Installing src/helloworld... done
[helloworld] Removing unneeded app content...
Removing unneeded app bundle content... done
[helloworld] Application updated.
[helloworld] Starting app...
package 命令也接受 -u
参数,因此如果你修改了应用程序代码并希望立即重新打包,可以运行 briefcase package -u
。
下一步¶
现在,我们的应用程序已经打包,可以在桌面平台上发布,我们也可以更新应用程序中的代码。
但是移动应用程序呢?在 Tutorial 5 中,我们将把应用程序转换为移动应用程序,并将其部署到设备模拟器和手机上。