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
Creating Virtual Environment
Let’s first create a virtual environment for
conda. If you do not know, how to setup conda for your machine, check out my other tutorial.
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
We have to install the following dependencies for
$ brew install pkg-config sdl2 sdl2_image sdl2_ttf sdl2_mixer gstreamer
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
Now, let’s install
$ conda install kivy -c conda-forge
- Pip Wheel
or you can install
$ python -m pip install kivy
$ python -m pip install kivy[base] kivy_examples --no-binary kivy
$ 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
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
# running the app if __name__=="__main__": HelloWorldKivyApp().run()
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: