Kivy Tutorial
Kivy Tutorial
In this tutorial, you will learn to build attractive GUI applications with Kivy
GUI library. This tutorial is a good starting point for anybody who wants to
develop cross-platform GUI apps.
Audience
This tutorial is a good starting point for anybody who wants to develop
cross-platform GUI apps. Kivy is also a good choice for mobile app
development for Android an iOS devices.
One can follow the examples in this tutorial to develop attractive desktop
GUI applications and mobile apps.
Prerequisites
Since kivy is a Python library, an intermediate level knowledge of Python
programming is required. Good understanding of principles of Object-
oriented programming will be an added advantage.
All the content and graphics published in this e-book are the property of
Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse,
retain, copy, distribute or republish any contents or a part of contents of
this e-book in any manner without written consent of the publisher.
We strive to update the contents of our website and tutorials as timely and
as precisely as possible, however, the contents may contain inaccuracies or
errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the
accuracy, timeliness or completeness of our website or its contents
including this tutorial. If you discover any errors on our website or in this
tutorial, please notify us at [email protected].
1
Python Kivy Tutorial
Table of Contents
About the Tutorial ........................................................................................................... 1
Audience ......................................................................................................................... 1
Prerequisites ................................................................................................................... 1
Disclaimer & Copyright ................................................................................................... 1
Table of Contents ............................................................................................................ 2
2
Python Kivy Tutorial
3
Python Kivy Tutorial
4
Python Kivy Tutorial
5
Python Kivy Tutorial
6
1. Kivy – Getting Started
Python Kivy Tutorial
PyQt5: This library is a Python port for the Qt GUI toolkit. Our
extensive tutorial on PyQt5 can be accessed here.
7
Python Kivy Tutorial
Kivy relies upon Cython for its core implementation, and SDL2
(Simple DirectMedia Layer) for low-level multimedia and input
handling.
The initial version of Kivy library was released in 2011. Currently, Kivy
version 2.2 is available, which has been released in May 2023.
8
2. Kivy – Installation Python Kivy Tutorial
On Ubuntu Linux, update the APT repo and install "venv", if required, before
creating a virtual environment.
C:\>cd kivyenv
C:\kivyenv>scripts\activate
(kivyenv) C:\kivyenv>
9
Python Kivy Tutorial
mvl@GNVBGL3:~$ cd kivyenv
mvl@GNVBGL3:~/kivyenv$ source bin/activate
(myenv) mvl@GNVBGL3:~/kivyenv$
On macOS, you can install SDL2 using Homebrew by running the following
command in your terminal:
If on Linux OS, use the corresponding package manager to install SDL2. For
example, it is done with the following command on Ubuntu Linux machine:
10
Python Kivy Tutorial
You can also obtain the list of all the packages installed using the "pip
freeze" command:
11
Python Kivy Tutorial
Pygments==2.15.1
pypiwin32==223
pywin32==306
requests==2.31.0
urllib3==2.0.2
12
3. Kivy – ArchitecturePython Kivy Tutorial
Core Providers
An important feature of Kivy architecture is "modularity" and "abstraction".
The actions such as opening a window, reading audio and video stream,
loading images, etc., are the core tasks in any graphical application. Kivy
abstracts these core tasks by providing easy to implement API to the drivers
that control the hardware.
Kivy uses specific providers for operating system on which your app is being
run. Each operating system (Windows, Linux, MacOS, etc.) has its own
native APIs for the different core tasks. The act as an intermediate
communication layer between the operating system on one side and to Kivy
on the other. Thus Kivy fully leverages the functionality exposed by the
operating system to enhance the efficiency.
13
Python Kivy Tutorial
Input Providers
An input provider is a piece of code that adds support for a specific input
device. The different input devices having built-in support in Kivy include:
To add support for a new input device, provide a new class that reads your
input data from your device and transforms them into Kivy basic events.
Graphics
OpenGL is base of the entire Graphics API of Kivy framework. OpenGL
instructions are used by Kivy to issue hardware-accelerated drawing
commands. Kivy does away the difficult part of writing OpenGL commands
by defining simple-to-use functionality.
Kivy uses OpenGL version 2.0 ES (GLES or OpenGL for embedded systems)
with which you can undertake cross-platform development.
Core Library
The high level abstraction is provided by the following constituents of Kivy
framework:
Clock: the Clock API helps you to schedule timer events. Both one-
shot timers and periodic timers are supported.
14
Python Kivy Tutorial
UIX
Kivy's user interface is built with widgets and layouts.
Widgets are the UI elements that you add to your app to provide
some kind of functionality. Examples of widget include buttons,
sliders, lists and so on. Widgets receive MotionEvents.
Event Dispatch
The term "widget" is used for UI elements in almost all graphics toolkits.
Any object that receives input events is a widget. One or more widgets are
arranged in a tree structure.
The Kivy app window can hold only one root widget, but the root widget can
include other widgets in a tree structure. As a result, there is a "parent-
children-sibling" relationship amongst the widgets.
Whenever a new input event occurs, the root widget of the widget tree first
receives the event. Depending on the state of the touch, the event is
propagated down the widget tree.
Each widget in the tree can either process the event or pass it to the next
widget in hierarchy. If a widget absorbs and process the event, it should
return True so that its propagation down the tree is stopped and no further
processing will happen with that event.
Since the event propagates through the widget tree, it is often necessary
to verify if the event has occurred in the area of a certain widget that is
expected to handle it. The collide_point() method can help in ascertaining
this fact. This method checks if the touch position falls within the 'watched
area' of a certain widget and returns True or False otherwise. By default,
this checks the rectangular region on the screen that's described by the
widget's pos (for position; x & y) and size (width & height).
15
4. Kivy – File Syntax Python Kivy Tutorial
The UI design is defined in a text file which must have a ".kv" extension. It
contains the hierarchical sequence of widgets in the application window. The
file adapts a tree-like structure, showing the parent-child-sibling
relationship among the widgets. Below each widget, its properties, events
and event handlers are specified.
It must match with the main class in your application. This class is
inherited from the App class.
If the name of the class ends with "app" or "App" (for example,
HelloApp), the ".kv" file must exclude "app" from its name. It means,
for HelloApp class, the name of the ".kv" file must be "hello.kv".
The ".kv" file must be in the same folder in which the Python
application file (.py) is present.
While using the ".kv" file, the App class doesn't override the build() method.
Declaring a class simply with a pass statement is enough. When the run() method
is invoked, Kivy automatically loads the UI from the respective ".kv" file.
Let us first remove the build() method from the HelloApp class:
class HelloApp(App):
pass
app = HelloApp()
app.run()
16
Python Kivy Tutorial
The User interface is defined in "hello.kv" file in the same folder. We have
a top level BoxLayout with vertical orientation, under which two labels are
placed. Save the following script as "hello.kv" file
BoxLayout:
orientation: 'vertical'
Label:
text: 'Python Kivy Tutorial'
font_size: '30pt'
Label:
text: 'From TutorialsPoint'
font_size: '50'
color: (1,0,0,1)
Now, if you run the "hello.py" program, it will produce the following output:
In latter chapters, we shall learn how to add event handlers to the widgets
in the ".kv" file.
==========
End of ebook preview
If you liked what you saw…
Buy it from our store @ https://store.tutorialspoint.com
17