Python Kivy is an open-source Python library designed for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps. It runs on Linux, Windows, OS X, Android, iOS, and Raspberry Pi. One of Kivy’s standout features is its ability to handle multi-touch events, making it an excellent choice for mobile app development. This guide provides a detailed introduction to Kivy, including how to set it up, its core concepts, and example code to get you started on developing mobile applications.
Setting Up Kivy
Before you start using Kivy, you need to install it. Kivy requires Python, so ensure you have Python installed on your system.
Installation
- Install Kivy on Windows: Open a command prompt and type:
pip install kivy[base] kivy_examples
Verifying the Installation
To verify that Kivy has been installed correctly, you can run a simple Kivy application. Create a new Python file and add the following code:
from kivy.app import App
from kivy.uix.label import Label
class MyApp(App):
def build(self):
return Label(text="Hello, Kivy!")
if __name__ == "__main__":
MyApp().run()
Run the script, and you should see a window displaying “Hello, Kivy!”.
Output:

Core Concepts of Kivy
Widgets
Widgets are the building blocks of a Kivy application. They represent GUI elements such as buttons, labels, sliders, etc. Widgets can be nested to create complex layouts.
Layouts
Layouts are used to arrange widgets in a specific manner. Kivy provides several types of layouts, including BoxLayout, GridLayout, StackLayout, and FloatLayout. Each layout arranges its children widgets in a different way.
Events
Events in Kivy are actions that are triggered by user interactions, such as touching the screen, pressing a button, or typing on the keyboard. Kivy handles these events using the event dispatcher system.
Creating a Simple Mobile App
Let’s create a simple mobile app using Kivy that includes buttons, labels, and a layout.
Step 1: Define the App Class
First, we define our application class by inheriting from App. This class will contain the main logic of our app.
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
class SimpleApp(App):
def build(self):
# Create a box layout
layout = BoxLayout(orientation='vertical')
# Create a label
self.label = Label(text="Hello, Kivy!")
layout.add_widget(self.label)
# Create a button
button = Button(text="Press Me")
button.bind(on_press=self.on_button_press)
layout.add_widget(button)
return layout
def on_button_press(self, instance):
self.label.text = "Button Pressed!"
if __name__ == "__main__":
SimpleApp().run()
Output:
In this example, we create a BoxLayout to arrange our widgets vertically. We add a Label and a Button to the layout. When the button is pressed, the on_button_press method is called, updating the label text.
Adding More Functionality
Let’s enhance our app by adding more widgets and functionalities.
Step 2: Adding More Widgets
We will add a TextInput widget to allow users to enter text, and a Slider to let users adjust a value.
from kivy.uix.textinput import TextInput
from kivy.uix.slider import Slider
class EnhancedApp(App):
def build(self):
layout = BoxLayout(orientation='vertical')
self.label = Label(text="Hello, Kivy!")
layout.add_widget(self.label)
self.text_input = TextInput(hint_text="Enter your name")
layout.add_widget(self.text_input)
self.slider = Slider(min=0, max=100, value=50)
self.slider.bind(value=self.on_slider_value_change)
layout.add_widget(self.slider)
button = Button(text="Submit")
button.bind(on_press=self.on_button_press)
layout.add_widget(button)
return layout
def on_button_press(self, instance):
name = self.text_input.text
self.label.text = f"Hello, {name}!"
def on_slider_value_change(self, instance, value):
self.label.text = f"Slider value: {int(value)}"
if __name__ == "__main__":
EnhancedApp().run()
Output:
In this enhanced version, the app now includes a TextInput for entering a name and a Slider to adjust a value. The on_slider_value_change method updates the label text with the current slider value.
Let us take another example to create a to do list app
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.checkbox import CheckBox
from kivy.uix.scrollview import ScrollView
class ToDoListApp(App):
def build(self):
self.tasks = []
# Main layout
main_layout = BoxLayout(orientation='vertical', padding=10, spacing=10)
# Title
title_label = Label(text="To-Do List", font_size='24sp')
main_layout.add_widget(title_label)
# Input box for new tasks
self.input_box = TextInput(hint_text="Enter a new task", size_hint_y=None, height=40)
main_layout.add_widget(self.input_box)
# Button to add tasks
add_task_button = Button(text="Add Task", size_hint_y=None, height=40)
add_task_button.bind(on_press=self.add_task)
main_layout.add_widget(add_task_button)
# ScrollView to hold the tasks
self.task_list = BoxLayout(orientation='vertical', spacing=10, size_hint_y=None)
self.task_list.bind(minimum_height=self.task_list.setter('height'))
scroll_view = ScrollView(size_hint=(1, None), size=(Window.width, Window.height - 150))
scroll_view.add_widget(self.task_list)
main_layout.add_widget(scroll_view)
return main_layout
def add_task(self, instance):
task_text = self.input_box.text.strip()
if task_text == "":
return
# Layout for each task
task_layout = BoxLayout(orientation='horizontal', size_hint_y=None, height=40)
# Checkbox for marking task as complete
checkbox = CheckBox(size_hint_x=None, width=40)
task_layout.add_widget(checkbox)
# Label to display the task
task_label = Label(text=task_text)
task_layout.add_widget(task_label)
# Button to remove the task
remove_button = Button(text="Remove", size_hint_x=None, width=100)
remove_button.bind(on_press=lambda x: self.remove_task(task_layout))
task_layout.add_widget(remove_button)
self.task_list.add_widget(task_layout)
self.tasks.append((checkbox, task_label))
self.input_box.text = ""
def remove_task(self, task_layout):
self.task_list.remove_widget(task_layout)
if __name__ == "__main__":
from kivy.core.window import Window
Window.size = (360, 640) # Set the size for mobile app preview
ToDoListApp().run()
Output:

Explanation
- App Structure: The app uses a vertical
BoxLayoutas the main layout, which contains a title, an input box for adding new tasks, a button to add tasks, and aScrollViewto display the tasks. - Adding Tasks: When the “Add Task” button is pressed, the
add_taskmethod is called. This method creates a new horizontalBoxLayoutfor the task, including aCheckBoxto mark the task as complete, aLabelto display the task text, and a “Remove” button to delete the task. - Removing Tasks: Each task has a “Remove” button that, when pressed, calls the
remove_taskmethod to remove the task layout from the task list. - Scrolling: The tasks are contained within a
ScrollViewto ensure the app can handle a long list of tasks.
Running the App
To run the app, save the code to a file, for example, todo_list_app.py, and execute the file using Python:
python todo_list_app.py
Deploying Kivy Apps on Mobile Devices
To deploy Kivy applications on mobile devices, you can use tools like Buildozer for Android and Pyobjus for iOS.
Deploying on Android using Buildozer
- Install Buildozer:
pip install buildozer
Initialize Buildozer:
buildozer init
Build the APK:
Configure Buildozer: Edit the buildozer.spec file generated by the previous command to configure your application.
buildozer -v android debug
- Deploy the APK: Once the build is complete, you can find the APK file in the
bindirectory. Transfer this APK to your Android device and install it.
Conclusion
Kivy is a powerful and flexible framework for developing multi-touch applications, particularly for mobile devices. Its ability to create custom, dynamic user interfaces and handle multi-touch events makes it an excellent choice for mobile app development. This guide introduced you to the basics of Kivy, including how to set it up, its core concepts, and creating a simple mobile app with example code. With Kivy, you can leverage the power of Python to develop robust and feature-rich mobile applications.





Leave a Reply