Make Mobile App using Python

Introduction to Kivy

Posted by Afsal on 26-May-2023

Hi Pythonistas!

Today we will start another series of post. In this we will learn to make mobile application using Kivy. Kivy is a Python framework that allows you to build multi-touch applications for desktop and mobile platforms. In this tutorial, we will walk through the process of creating a simple "Hello, World!" program using Kivy.

Installation

pip install kivy

Code

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

class HelloWorldApp(App):
    def build(self):
        return Label(text='Hello, World!')

if __name__ == '__main__':
    HelloWorldApp().run()

App class is a fundamental component and is typically used as the base class for creating applications. It provides essential functionality and acts as the main entry point for your Kivy application.Then there will be one or more widget on this App. A sample structure of a kivy app will be like this

Kivy App
|
|__ Root Widget (e.g., a Layout)
	|
	|__ Child Widgets
    	|
    	|__ Sub-child Widgets
    	|
    	|__ Sub-child Widgets
	|
	|__ Child Widgets
	|
	|__ Child Widgets

build method is responsible for the UI part of the App. Here we have added a Label widget to show the helloworld text.

So now we have an App and a text widget on it. The content of the text widget is helloworld.

Then we start the event loop using run method. It's important to note that once the run() method is called, it typically blocks the execution of subsequent code until the application is terminated. Therefore, any code placed after the run() call will only be executed when the application exits.

If we run this program we get output like this

Hope you have learned basic of kivy app and it’s structure. In our next post we will learn how to handle event like click. Please share your suggestions with afsal@parseltongue.co.in