What is Kivy?
Kivy is a free, open source Python library that allows for quick and easy development of highly interactive multiplatform applications.
Kivy’s execution speed is comparable to the native mobile alternative, Java for Android or Objective C for iOS. Moreover, Kivy has the huge advantage of being able to run on multiple platforms, just as HTML5 does; in which case, Kivy performs better because it doesn’t rely on a heavy browser, and many of its components are implemented in C using the Cython library in such a way that most of the graphics processing runs directly in the GPU.
Knowledge you need to get started with Kivy
This tutorial requires some knowledge of Python, and very basic terminal skills, but also it requires some understanding of Object-Oriented Programming (OOP) concepts. If you are not good with the classes and object oriented programming, don’t worry, just go through my Object Oriented Programming tutorials, so that you don’t find any difficulties in future.
Let’s create the most basic app with kivy:
from kivy.app import App from kivy.uix.button import Label class hello(App): def build(self): return Label(text="Hello, World") if __name__=='__main__': hello().run()
This is a very simple Python code. Launching a Kivy program is not any different from launching any other Python application.
So, is Kivy just another library for Python? Well, yes. But as part of the library, Kivy offers its own language in order to separate the logic from the presentation and to link elements of the interface.
Moreover, remember that this library will allow you to port your applications to many platforms. Let’s start to explore the Kivy language. We will separate the previous Python code into two files, one for the presentation (interface), and another for the logic. The first file includes the Python lines:
file name – hello.py
from kivy.app import App from kivy.uix.button import Label class hello(App): def build(self): return Label() if __name__=='__main__': hello().run()
The hello.py code is very similar to our previous. The difference is that the method build(self) doesn’t have the Hello World! message. Instead, the message has been moved to the text property in the Kivy language file (hello.kv).
file name- hello.kv
<Label>: text: 'Hello, World!'
You might wonder how Python or Kivy knows that these two files (hello.py and hello.kv) are related. This tends to be confusing at the beginning. The key is in the name of the subclass of App, which in this case is hello.
Once that consideration is included, this example can be run in the same way we ran the previous one. We just need to be sure we are calling the main file – python hello.py .
It will give us the same output as above.
That’s it for today we will continue in the next chapter.