Kivy

Notes on how to install and configure kivy. Kivy allows to develop applications in python for android, ios, windows, mac and linux.

This notes are based on; Build a Mobile Application With the Kivy Python Framework.


Virtual environment

Virtual environment allows to have different libraries / modules from the ones installed on the system. They are independent from the machine and from project to project.

$ mkdir python-virtual-environments
$ cd python-virtual-environments
$ python -m venv env

A new env folder is created, to activate the environment activate it with;

$ source env/bin/activate
(env) $

Env on prompt indicates that environment is active, from now on python will use this environment packages and settings. To exit environment type deactivate;

(env) $ deactivate
$

Activate environment, if you run which python it will give the path to executable inside the environment. Another way to understand how do this work is to echo $PATH.

Virtualenvwrapper tool helps to manage virtual environments by placing them on one place and make it easy to create, delete and copy environments.

$ prt-get depinst python3-virtualenvwrapper

Activate functions contained in virtualenvwrapper.sh to use virtualenvwrapper;

$ whereis virtualenvwrapper.sh
virtualenvwrapper: /usr/bin/virtualenvwrapper.sh

Add that file to ~/.profile or ~/.bashrc so it's loaded every time a shell is launched;

VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.7m
source /usr/bin/virtualenvwrapper.sh

Source the file;

source ~/.profile

Now there is new commands available;

  • workon
  • deactivate
  • mkvirtualenv
  • cdvirtualenv
  • rmvirtualenv

For more information about this commands check documentation.

To start a new project and activate virtual environment;

$ mkvirtualenv new-project-name

And to exit virtual environment;

(new-project-name) $ deactivate

To list virtual environments;

$ workon

To activate one;

$ workon new-project-name


Hello World

Create a new virtual environment for a hello world and install kivy;

$ mkvirtualenv kivy-hello-world
(kivy-hello-world) $ python3 -m pip install kivy

Among other messages it should display;

Successfully installed Kivy-Garden-0.1.4 certifi-2020.4.5.1 chardet-3.0.4 docutils-0.16 idna-2.9 kivy-1.11.1 pygments-2.6.1 requests-2.23.0 urllib3-1.25.9

Above versions can be different since the time of this writing. Create the file hello_world.py with following content;

from kivy.app import App
from kivy.uix.label import Label

class MainApp(App):
    def build(self):
        label = Label(text='Hello from Kivy',
                      size_hint=(.5, .5),
                      pos_hint={'center_x': .5, 'center_y': .5})

        return label

if __name__ == '__main__':
    app = MainApp()
    app.run()

And run it;

(kivy-hello-world) $ python3 hello_world.py


Packaging

Make sure system have all the packages necessary;

$ sudo prt-get depinst git zip unzip jdk python3-pip libtinfo-compat cmake libffi texlive

To package the application is used buildozer, inside the virtual environment install it;

(kivy-hello-world) $ pip3 install buildozer

Now run buildozer init, this will create buildozer.spec file used to specify app details;

(kivy-hello-world) $ buildozer init
File buildozer.spec created, ready to customize!
(kivy-hello-world) $

Edit following fields on buildozer.spec (change others if you wish);

[app]

# (str) Title of your application
title = Hello World

# (str) Package name
package.name = helloworld

Create a new folder and rename app to main.py;

(kivy-hello-world) $ mkdir hello_world
(kivy-hello-world) $ mv hello_world.py hello_world/main.py
(kivy-hello-world) $ mv buildozer.spec hello_world/
(kivy-hello-world) $ cd hello_world
(kivy-hello-world) $ pip3 install --upgrade cython virtualenv

Add the following to ~/.bashrc and source it;

export PATH=$PATH:~/.local/bin

Package your app for android;

(kivy-hello-world) $ buildozer -v android debug