【转载】Python+Qt程序部署工具fbs教程:fbs tutorial

11 篇文章 0 订阅

链接地址:
https://build-system.fman.io/docs/

fbs tutorial

This tutorial shows how you can use fbs to create a simple Python GUI and an associated installer.

You can follow this tutorial on Windows, Mac or Linux. The only prerequisite is Python 3.5 or 3.6. Python 3.7 is not yet officially supported. It may or may not work.

Setup

Create a virtual environment in the current directory:

python3 -m venv venv

Activate the virtual environment:

# On Mac/Linux:
source venv/bin/activate
# On Windows:
call venv\scripts\activate.bat

The remainder of the tutorial assumes that the virtual environment is active.

Install the required libraries (most notably, fbs and PyQt5):

pip install fbs PyQt5==5.9.2

(If this produces errors, try pip install wheel first.)

You can also use Qt for Python instead of PyQt. To do this, simply write
PySide2 instead of PyQt5 throughout this tutorial. For the above, use pip install PySide2==5.12.0.

Start a project

Execute the following command to start a new fbs project:

fbs startproject

This asks you a few questions. You can for instance use Tutorial as the app name and your name as the author.

The command creates a new folder called src/ in your current directory.
This folder contains the minimum configuration for a bare-bones PyQt app.

Run the app

To run the basic PyQt application from source, execute the following command:

fbs run

Source code of the sample app

Let’s now take a look at the source code of the PyQt app that was generated.
It is at src/main/python/main.py:

from fbs_runtime.application_context.PyQt5 import ApplicationContext
from PyQt5.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)

The important steps are highlighted as comments. They’re the only boilerplate that’s required. In the middle of the code, you can see that a window is being created, resized and then shown.

Freezing the app

We want to turn the source code of our app into a standalone executable that can be run on your users’ computers. In the context of Python applications, this process is called “freezing”.

Use the following command to turn the app’s source code into a standalone executable:

fbs freeze

This creates the folder target/Tutorial. You can copy this directory to any other computer (with the same OS as yours) and run the app there! Isn’t that awesome?

Creating an installer

Desktop applications are normally distributed by means of an installer.
On Windows, this would be an executable called TutorialSetup.exe.
On Mac, mountable disk images such as Tutorial.dmg are commonly used.
On Linux, .deb files are common on Ubuntu, .rpm on Fedora / CentOS, and .pkg.tar.xz on Arch.

fbs lets you generate each of the above packages via the command:

fbs installer

Depending on your operating system, this may require you to first install some tools. Please read on for OS-specific instructions.

Windows installer

Before you can use the installer command on Windows, please install NSIS and add its installation directory to your PATH environment variable.

The installer is created at target/TutorialSetup.exe. It lets your users pick the installation directory and adds your app to the Start Menu. It also creates an entry in Windows’ list of installed programs. Your users can use this to uninstall your app.

Linux installer

On Linux, the installer command requires that you have
fpm. You can for instance follow [these instructions (https://fpm.readthedocs.io/en/latest/installing.html) to install it.

Depending on your Linux distribution, fbs creates the installer at
target/Tutorial.deb, ...pkg.tar.xz or ...rpm. Your users can use these files to install your app with their respective package manager.

Summary

fbs lets you use Python and Qt to create desktop applications for Windows, Mac and Linux. It can create installers for your app, and automatically handles the packaging of third-party libraries and data files. These things normally take weeks to figure out. fbs gives them to you in minutes instead.

Where to go from here

fbs’s Manual explains the technical foundation of the steps in this tutorial. Read it to find out more about fbs’s required directory structure, dependency management, handling of data files,custom build commands, API and more.

If you have not used PyQt before: It’s the library that allowed us in the above examples to use Qt (a GUI framework) from Python. fbs’s contribution is not to combine Python and Qt. It’s to make it very easy to package and deploy PyQt-based apps to your users’ computers. For an introduction to PyQt, see
here.

Feel free to share the link to this tutorial! If you are not yet on fbs’s mailing list and want to be notified as it evolves,sign up here.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值