2 minute read

If you know python and are interested in building mobile applications, but do not have knowledge about Android or IOS application development, this post is for you.

kivy is an excellent mobile application development platform for python programmers. In this tutorial, we will learn to build our first mobile application by printing out Hello World on our first application, which is written in python with kivy module.

Install kivy

Creating Virtual Environment

Let’s first create a virtual environment for kivy using conda. If you do not know, how to setup conda for your machine, check out my other tutorial.

# Data Science Work Environment setup on Linux/Mac

Now, let’s create an enviornment named kivy (or whatever name you prefer) based on python 3.7 (or whatever version you prefer).

$ conda create --name kivy python=3.7

Installing Dependencies

We have to install the following dependencies for macOS.

$ brew install pkg-config sdl2 sdl2_image sdl2_ttf sdl2_mixer gstreamer

or for Ubuntu

sudo apt-get install -y \
    libsdl2-dev \
    libsdl2-image-dev \
    libsdl2-mixer-dev \
    libsdl2-ttf-dev \
    libportmidi-dev \
    libswscale-dev \
    libavformat-dev \
    libavcodec-dev \
    zlib1g-dev

or for fedora-alike OS

sudo dnf install -y ffmpeg-libs SDL2-devel SDL2_image-devel SDL2_mixer-devel \
SDL2_ttf-devel portmidi-devel libavdevice libavc1394-devel zlibrary-devel ccache \
mesa-libGL mesa-libGL-devel

Install Kivy

  1. Conda Now, let’s install kivy using conda
     $ conda install kivy -c conda-forge
    
  2. Pip Wheel or you can install kivy using pip
     $ python -m pip install kivy
    

    stable version

     $ python -m pip install kivy[base] kivy_examples --no-binary kivy
    

    latest version

     $ python -m pip install "kivy[base] @ https://github.com/kivy/kivy/archive/master.zip"
    

Creating First App

Now let’s follow the steps to build our first app

Step-by-step Guide

First, we will import the kivy module. We will also import the App class that features all the necessary underlying work for our app. And, because we will only print in a label, we will import Label. For other features (e.g., button), we can import those manually.

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

Now, let’s create a class that inherits properties from the App class. We need to have build method that returns whatever we want to show while running the program. In this instance, we create a label with our hello world! text.

# Defining a class
class HelloWorldKivyApp(App):
	# our primary widget
	def build(self):
		# Label with our text
		return Label(text ="Hello World!")		

Now, we can run the program using the inherited method run().

# running the app
if __name__=="__main__":
	HelloWorldKivyApp().run()			

Full Code

Here’ is the full code:

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

# Defining a class
class HelloWorldKivyApp(App):
	# our primary widget
	def build(self):
		# Label with our text
		return Label(text ="Hello World!")		


# running the app
if __name__=="__main__":
	HelloWorldKivyApp().run()			

create a new file

$ nano myfirstapp.py

copy and paste the above program and run

$ python myfirstapp.py

You will see the following output:


References

  1. Getting StartedGetting Started » Installing Kivy
  2. Kivy Installation on Linux

Leave a comment