内容表

上一话题

Qt for Python 部署

下一话题

Qt for Python & PyInstaller

Qt for Python & fbs

fbs 为打包、创建安装程序及签名应用程序提供强大环境。它还使您能够管理应用程序更新。由于 fbs 基于 PyInstaller,它支持 Linux、macOS 及 Windows。

更多细节,见 fbs tutorial fbs manual .

预备

安装 fbs (>= 0.7.6) 是凭借 pip :

pip install fbs
									

若使用的是虚拟环境,记住要先将其激活再安装 fbs .

安装后,可以使用 fbs 可执行文件。

开始新工程

fbs 采用以下命令为您提供创建基工程结构的有用特征:

fbs startproject
									

此命令提示回答一些问题以配置工程细节,像:

  • 应用程序名称

  • 作者姓名

  • Qt 绑定 (PySide2 或 PyQt5)

  • 捆绑标识符 (用于 macOS)

Afterwards, you have a src/ directory that contains the following structure:

└── src
    ├── build
    │   └── settings
    └── main
        ├── icons
        │   ├── base
        │   ├── linux
        │   └── mac
        └── python
									

Inside the settings directory, there are a few JSON files that can be edited to include more information about your project.

main file is in the python directory, and its default content is:

from fbs_runtime.application_context import ApplicationContext
from PySide2.QtWidgets import QMainWindow
import sys
if __name__ == '__main__':
    appctxt = ApplicationContext()       # 1. Instantiate ApplicationContext
    window = QMainWindow()
    window.resize(250, 150)
    window.show()
    exit_code = appctxt.app.exec_()      # 2. Invoke appctxt.app.exec_()
    sys.exit(exit_code)
									

此范例展示空 QMainWindow . You can run it using the following command:

fbs run
									

冻结应用程序

Once you’ve verified that the application is working properly, you can continue with the freezing process using the following command:

fbs freeze
									

After the process completes, you see a message stating the location of your executable. For example:

Done. You can now run `target/MyApp/MyApp`. If that doesn't work, see
https://build-system.fman.io/troubleshooting.
									

Now, you can try to run the application. The result is the same window as the one you saw with the fbs run 命令:

cd target/MyApp/
./MyApp
									

注意

This is the case for Linux. For other platforms like macOS, you need to enter the directory: target/MyApp.app/Contents/macOS . For Windows, you need to find the MyApp.exe 可执行文件。