• Stars
    star
    1,857
  • Rank 24,084 (Top 0.5 %)
  • Language
    Python
  • License
    GNU General Publi...
  • Created over 6 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Tutorial for creating Python/Qt GUIs with fbs

fbs tutorial

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

Screenshot of sample app on Windows Windows installer Screenshot of installer on Mac

You can follow this tutorial on Windows, Mac or Linux. You need Python 3.5 or later.

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):

pip install fbs-tutorial

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

We are using PyQt in this tutorial. In other fbs projects, you can use PySide just as well.

Start a project

Execute the following command to start a new fbs project:

fbs startproject

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

Run the app

To run our little application from source, execute the following command:

fbs run

This shows an (admittedly not very exciting) window. Screenshots on Windows/Mac/Ubuntu:

Screenshot of sample app on Windows Screenshot of sample app on Mac Screenshot of sample app on Ubuntu

Source code of the sample app

Let us now take a look at the source code that was generated by fbs. 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/YourApp. 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 YourAppSetup.exe. On Mac, mountable disk images such as YourApp.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/YourAppSetup.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. The following screenshots show these steps in action:

Mac installer

On Mac, the installer command generates the file target/YourApp.dmg. When your users open it, they see the following volume:

Screenshot of installer on Mac

To install your app, your users simply drag its icon to the Applications folder (also shown in the volume).

Linux installer

On Linux, the installer command requires that you have fpm. You can for instance follow these instructions to install it.

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

A more interesting example

We will now create a more powerful example. Here's what it looks like on Windows:

Quote app

When you click on the button in the window, a new quote is fetched from the internet and displayed above.

Before you can run this example, you need to install the Python requests library. To do this, type in the following command:

pip install requests

The source code of the new app consists of two files:

Please copy the former over the existing file in src/main/python/, and the latter into the new directory src/main/resources/base/.

Once you have followed these steps, you can do fbs run (or fbs freeze etc.) as before.

The new app uses the following code to fetch quotes from the internet:

def _get_quote():
    return requests.get('https://build-system.fman.io/quote').text

You can see that it uses the requests library we just installed above. Feel free to open build-system.fman.io/quote in the browser to get a feel for what it returns. Its data comes from a public database.

The app follows the same basic steps as before. It instantiates an application context and ends by calling appctxt.app.exec_():

appctxt = ApplicationContext()
...
exit_code = appctxt.app.exec_()
sys.exit(exit_code)

What's different is what happens in between:

stylesheet = appctxt.get_resource('styles.qss')
appctxt.app.setStyleSheet(open(stylesheet).read())
window = MainWindow()
window.show()

The first line uses get_resource(...) to obtain the path to styles.qss. This is a QSS file, Qt's equivalent to CSS. The next line reads its contents and sets them as the stylesheet of the application context's .app.

fbs ensures that get_resource(...) works both when running from source (i.e. during fbs run) and when running the compiled form of your app. In the former case, the returned path is in src/main/resources. In the latter, it will be in your app's installation directory. fbs handles the corresponding details transparently.

The next-to-last line instantiates MainWindow. This new class sets up the text field for the quote and the button. When the button is clicked, it changes the contents of the text field using _get_quote() above. You can find the full code in main.py.

As already mentioned, you can use fbs run to run the new app. But here's what's really cool: You can also do fbs freeze and fbs installer to distribute it to other computers. fbs includes the requests dependency and the styles.qss file automatically.

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 Python and Qt-based apps to your users' computers. For an introduction to PyQt, see here.

Feel free to share the link to this tutorial!

More Repositories

1

fbs

Create Python GUIs with Qt in minutes
Python
3,624
star
2

selenium-python-helium

Selenium-python but lighter: Helium is the best Python library for web automation.
Python
3,428
star
3

sentry-self-hosted

Self-host Sentry for $5 per month
Shell
158
star
4

gitignore_parser

A spec-compliant gitignore parser for Python 3.5+
Python
119
star
5

java-generator-functions

An implementation of a Python-like yield(...) method in Java.
Java
88
star
6

matrixlock

Replace i3's lock screen by the Matrix.
Python
66
star
7

timer-cm

A Python context manager for measuring execution time
Python
45
star
8

fullcalendar-rightclick

Rightclick support for Adam Shaw's FullCalendar jQuery plugin.
HTML
36
star
9

pypxlib

Python bindings for the pxlib library for reading and writing Paradox databases.
Python
32
star
10

fullcalendar-columns

A FullCalendar extension that adds support for multiple columns (resources) per day.
JavaScript
28
star
11

pyqt-resources

Tips and code snippets for PyQt developers
Python
27
star
12

django-404-middleware

An alternative to Django's BrokenLinkEmailsMiddleware
Python
17
star
13

ArrowNavigation

Navigate with the arrow keys (left: up, right: open directory)
Python
17
star
14

Preview

Preview files with F3
Python
13
star
15

QuickLook

Call macOS's Quick Look (Shift+Space)
Python
11
star
16

fix

Fix any failing command with ChatGPT
Python
11
star
17

ProcessFS

Manage processes with the command 'Show processes'
Python
8
star
18

kph

Kรถlner phonetik in Python
Python
7
star
19

MacOpen

Call macOS's native open command (โŒ˜+Enter)
Python
5
star
20

SynchronizedBrowsing

Mirror your navigation steps in the opposite pane
Python
5
star
21

ChangeLocation

Launch GoTo with the current location (Cmd/Alt+D)
Python
4
star
22

SwitchPanesWithArrowKeys

Switch panes with the arrow keys (left: left pane, right: right pane)
3
star
23

celery-error-emails

Automatically send error emails about failing Celery tasks in your Django project
Python
1
star
24

chat

A simple chat application for Windows, Mac and Linux
Python
1
star
25

ExcludeMyIP

A Google Analytics tool to exclude your own (home/work) IP
Python
1
star
26

fbs-tutorial-shim

Helper for mherrmann/fbs-tutorial
Python
1
star