Python

Kivy Python Tutorial

Kivy Python Tutorial
The importance of mobile software in our world today can never be overemphasized, everyone moves about with their devices regardless of the operating system, and for the devices to be useful, there's a need for software to help to carry out our daily tasks.

The Android operating system is arguably one of the most used operating systems on mobile devices today, and it is very efficient as well thanks to its affiliations with the Linux operating system. In this article, we are going to discuss how to build a sample android app with python.

So why Python?

We know languages like Java, Kotlin, frameworks like Xamarin, React Native are very efficient in the building of apps, but more often than not system admins are more conversant with using scripting languages such as Python for their tasks.

With Kivy, they can get to build minimal android apps for simple tasks on their Android devices without having to experience a change in syntax. Yes, we all know Python is not so fast when used in app development but who cares if it does the needed job?

With this, you can quickly write a web scraping script for example and compile into an android app and run it on the move; that's pretty cool.

To do this, we are going to be making use of a Python library called Kivy. Kivy is used to build cross-platform mobile apps, so it's not necessarily for android devices only it also supports the building of iOS and Windows software.


Installation of Kivy

Kivy is very easy to install, but things could go a bit haywire if the installed dependencies begin to clash.

To install Kivy, we can use the “pip' command for installing Python libraries, and we can use “apt-get” as well. For Kivy to work, it has a lot of dependencies especially when you are trying to make use of features such as the Camera, i.e., OpenCV, or another library such as Pillow.

However, you can get to do a simple installation of Kivy.

You can install Kivy for Python 2 with the command below:

sudo apt-get install python-kivy

Then Kivy for Python 3 can be installed with the command below:

sudo apt-get install python3-kivy

If you intend installing with the “pip” command, the command below will do the job:

pip install kivy

Then one very popular dependency which is pygame can be installed:

pip install pygame

If you intend to install the dependencies at this point, you can go ahead and install.

 For Ubuntu 16.04:

sudo apt-get install python-setuptools python-pygame python-opengl \
python-gst0.10 python-enchant gstreamer0.10-plugins-good python-dev \
build-essentialpython-pip libgl1-mesa-dev libgles2-mesa-dev zlib1g-dev

If you intend installing for other versions of Ubuntu, you can follow the steps from the Github documentation.

Before we proceed, you can confirm if the installation of Kivy is successful by importing the module from the interactive shell.

>>> import kivy
[INFO  ] [Logger  ] Record log in /data/user/0/ru.iiec.pydroid3/app_HOME/.kivy/
logs/kivy_18-02-26_0.txt
[INFO  ] [Kivy  ] v1.9.2-dev0
[INFO  ] [Python  ] v3.6.2 (default, Oct 15 2017, 09:18:13)
[GCC 7.2.0]
>>>

All you need is a result in this format; the numbers are not expected to tally.


Writing of code

We are going to be creating a simple app that displays some text on the screen.

Create a python file, which we would be naming “main.py”. This file would have the following content:

from kivy.app import App
class HelloApp(App):
 
pass
if __name__ == "__main__":
  HelloApp().run()

On the surface, it looks like it does nothing, but we would go through what each line of code does.

from kivy.app import App

This imports the App class from the kivy library which helps generate the application interface itself, asides that it has a lot of other properties to support the making of an app.

class HelloApp(App):
  pass

This creates a class HelloApp which inherits from the App which we imported earlier; we are not doing much here as all we have done is use the “pass” keyword.

So without typing any code in, it has all the methods of the App class.

if __name__ == "__main__":
  HelloApp().run()

Then we check to see if the Python script is run directly or being imported. If it runs directly, it executes the run() method of the App class that was inherited else nothing happens.

We are almost done just one more file. This is a kv file, which we would be using for our markup.

The kv file works in the kv language which has some similarity in syntax with Python.

Just create a new file without a name, and input the following lines of code.

Label:
   text:
       "Welcome To Linux Hint"

Looking at the main.py file, we would notice remember that we created a HelloApp() class which inherited from App and that was the only class.

In the kv file, then Label is automatically linked to the classes created in the python file. “Label” is used for displaying by using the box model.

The question is; how does our python file know that this file has the markup? It does this through the name.

Since our HelloApp class has two different words differentiated by the capitals, the kv file is expected to be named with the first word all in small letters and our file would be named hello.kv.

If our class is called LinuxApp or GameApp, our kv file would be named linux.kv and game.kv respectively.

Now, you can run your python file:

python main.py

You should get an output saying “Welcome To Linux Hint”.

This is just the tip of the iceberg of what you can do with the Kivy library; you can go through the full documentation here, you can also check out other examples as well.


Installing and using Buildozer

If you have followed this article from the beginning, you would recall that installing kivy we had to consider a lot of dependencies. Installing buildozer, on the other hand, is not as complicated.

All we would be doing is to clone the files from the GitHub repository, we install and then use.

git clone https://github.com/kivy/buildozer.git
cd buildozer
sudo python2.7 setup.py install

Here python2.7 would be the version of python that is installed on your system; for example, if you have python 3.5 installed you use Python3.5. Though some people claim to have issues using buildozer with Python 3, you can give it a try and if it fails you switch to Python 2.
After installation, you run the code below. Just like in the first case, Python2.7 can be changed to any version of Python it would be reasonable to use the version of python used to install buildozer.

python2.7 -m buildozer init

This creates a buildozer.spec file which contains the configuration settings for our app. While you can proceed without changing any of the configurations, you can check out the file and change things such as the application name, package name, etc.
The file should be in this format:

[app]
 
# (str) Title of your application
title = app
 
# (str) Package name
package.name = myapp
 
# (str) Package domain (needed for android/ios packaging)
package.domain = org.test
… .
… .

After this, you can get to compile your Android application, just like the first two instances you can change python2.7 to the version of python you have installed on your machine.

python2.7 buildozer android debug deploy run

If you are doing this for the first time, the needed Android SDK, Android NDK, and Android Ant files would be downloaded so you can get a cup of coffee as it may take some time depending on how fast your internet connection is.
When buildozer is done compiling the application, it saves it in the bin directory.
That's all for this tutorial; now you can create simple Android applications and run some scripts on your Android device.

How to reverse Mouse and Touchpads scrolling direction in Windows 10
Mouse and Touchpads not only make computing easy but more efficient and less time-consuming. We cannot imagine a life without these devices, but still...
How to change Mouse pointer and cursor size, color & scheme on Windows 10
The mouse pointer and cursor in Windows 10 are very important aspects of the operating system. This can be said for other operating systems as well, s...
Ilmaiset ja avoimen lähdekoodin pelimoottorit Linux-pelien kehittämiseen
Tämä artikkeli kattaa luettelon ilmaisista ja avoimen lähdekoodin pelimoottoreista, joita voidaan käyttää 2D- ja 3D-pelien kehittämiseen Linuxissa. Tä...