Privacy Policy Banner

We use cookies to improve your experience. By continuing, you agree to our Privacy Policy.

Kivy: a unique graphic cadrical in python

-

Kivy is a graphic caddiciel (framework), allowing to develop tactile interfaces (or usable in mouse) on all platforms, including mobiles. It is accompanied by a series of very practical software that we will present here.

logo depressed

Summary

Kivy: multi-platform graphic toolbox

Kivy allows you to create graphic interfaces naturally suited to touch screens (but which remain usable on a traditional desktop environment, mouse and without touch screen). He is written mainly in Python but the critical parts are written in cython, which allows him to have good performance.

A Kivy interface can be written in two ways (or, more commonly, by a combination of these two ways): in Python directly, or via kva declarative syntax which we are talking about below.

To give you an idea, here is an example of hello world Resumed from the official Kivy website:

from kivy.app import App from kivy.uix.button import Button  class TestApp(App):     def build(self):         return Button(text='Hello World')  TestApp().run()

What will give:
HELLO WORLD screenshot

An interface is made up of “widgets” arranged via “layouts”. Some widgets can easily set up common components today, such as the Carousel widget:
capture d’un widget Carousel

On the other hand, Kivy provides tools that simplify the life of the developer or developer, in particular a system of properties (not to be confused with Python properties) which allows to automatically update the interface when they are modified, or to easily attach a reminder function (“callback”), see below for a simple example. You can also cite a very practical and effective animation mechanism.

KV: declarative graphical interface

KV is a language to describe interfaces, it uses the properties mentioned above and the interface will be updated automatically when properties used in KV are modified.

Let’s take the hello seen above, and let’s do it with KV:

Widget:     Button:         text: "Hello World" 

Rather simple, right?
The KV code is generally put in separate files with the extension .kv.

The properties

Kivy therefore has a concept of property, which allow the binding of bidirectional data (Two-Way Data Binding), or in other to easily have the updated state between the Python code and the interface described in the file kv. It is very practical and easy use.

An example will probably help understand.

Let’s start by making a python virtual environment with Kivy:

$ mkdir demo  $ cd demo  $ python -m venv env  $ source env/bin/activate  $ pip install kivy

Then create the file demo.py following :

from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.properties import NumericProperty  class CompteurWidget(BoxLayout):     compteur = NumericProperty(0)      def increment(self):         self.compteur += 1  class DemoApp(App):     def build(self):         return CompteurWidget()  DemoApp().run()

And now, in the same directory, add the file demo.kv Next, the latter will be automatically discovered by Kivy because it is as our application (DemoApp) without the suffix App and in tiny:

 :     orientation: 'vertical'     padding: 20      Label:         text: str(root.compteur)         font_size: 30      Button:         text: "Incrémenter"         on_press: root.increment() 

We just have to launch the :

python demo.py

And to admire:

screenshot of the demo program

The label automatically updates when the counter compteur is incremented.

DLFP’s reader or dlfp reader can make an in -office.

Note: Do not stop to the fact that the IU is a bit “ugly” by default, there are much more attractive extensions (cf. KivyMD lower), and it is very easy to customize the interface and make a very beautiful one by changing its files a little .kv.

Plyer: easily access the material features of your device

PLYER is a for accessing many useful functions independently of the platform. So you have a common API to display a notification, for the state of the battery, vibrate the device, make the vocal text synthesis (“Text-to-Speech”), a file with the native dialogue of the platform, etc.

-

Although developed by the same team as Kivy, this package can be used independently and is therefore particularly useful for any multi-platform project. Refer to the project page to have a table of the features supported according to the platforms, and to the documentation for more details.

Python for Android: Use Python… on Android

If you want to distribute your application on Android, you will have to have a compiled Python version for this platform, as well as all the packages requiring compilation.

This is the role of Python for Android, which is a collection of tools that rely on the Android SDK to allow Kivy to be used or other Python packets on Android, and thus make your own Python distribution.

If you use a package which is not in pure python (that is to say that there are parts to compile) and which is not yet available or which is but in a too old version, you can write or update “recipes” (recipes “(recipes” (recipes “(recipes” (recipes in ) which indicate how to compile. Refer to documentation to see how to do it.

Note, however, that there is a penalty to use python compared to an application in a compiled language: you must load the interpreter at startup, and at the very launch of your application (and only at the first) there will be a decompression phase of your files. Apart from these small drawbacks, it works very well.

Again, the project is independent of Kivy and can be used with another Cadriciel.

Kivy for iOS: deploy on Apple devices

Like Android, the Kivy team provides the tools to make your Python for iOS distribution.

You will need to have an Apple machine to use them, because of the restrictions imposed by this platform (this is not specific to Kivy for iOS, but applies to all the tools to develop on iOS).

So with a single code base (and after spending a little time to correctly configure compilation tools), you can deploy your Kivy/Python application on all major platforms (except the web, for which there are other options like Brython, Pyodide or Pyscript, but it is a subject for another dispatch).

Pajnius: Use the Android Java API from Python

Sometimes the use of PLYER mentioned above and of the standard Python library is not enough, and you need to access the Android API, but the latter is made to be used with Java or Kotlin.

To be able to use the Android API from Python, Pajnius is available. This package provides access to Java classes as well as Python classes using JNI (“Java native interface” or “Java native interface”).

This package provides a method autoclass Who automatically converts a java into python, it is very easy to use and often sufficient for simple cases.

Kivymd is not an official project of the Kivy team, but a third -party project dedicated to the creation of a set of thermable widgets adopting Material Design.

Image of example from Kivymd from documentation

The image above comes from Kivymd’s documentation, you will also find video demos on the submission of the project.

Some limitations

A little note on my personal experience (Goffi). Kivy is an excellent Cadriciel, and the team is welcoming and responsive: it is a project that I would generally recommend. That said, in my particular case (an XMPP client with many features), I have some limitations that are currently pushing me to look for alternatives, especially based on QT (Pyqt and Pyside):

  • Accessibility is a major problem. The team works there, but today Kivy applications are not accessible. This is particularly due to the fact that the user interface is fully managed by Kivy, instead of using native components, and it is a completely blocking point for me.
  • There is no webview or a native HTML rendering, which is blocking because I make blogs.
  • The text rendering is incomplete, especially on mobile platforms. It is complicated to make a rendering with emojis, for example, a big problem for a messaging application these days.
  • Support for freedesktop portals is nonexistent: you have to do the implementation by hand, while it is native on major cadriciels like GTK or QT. I think for example of the opening or recording of file. Portals are particularly necessary if you want to see its application on Flatpak. More details on this ticket.
  • The Wayland support exists, but during my on GNU/Linux distributions on Pinephone, it did not and I could not display the application.
  • The loading of the Python interpreter takes a while, which makes the launch of the application on mobile platforms such as Android and probably iOS (which I have not tested) a little slow. Knowing that on these platforms the application can be killed at any time in which case it is necessary to restart everything, this can lead to a bad user experience. That said, with an optimized distribution (by removing unused packages), and on current phones which are very powerful, the problem is undoubtedly less.

Note that I did not have the opportunity to work with Kivy recently, maybe these remarks are no longer up to . Please indicate in comments if there has been evolution on these points.

Also, my use case is highly demanding (HTML rendering, effective text display, etc.). In most cases, Kivy fulfills all the boxes without problem (except accessibility, which remains its most important problem in my opinion).

Conclusion

As you can see, considerable work has been done on Kivy and its ecosystem, and it takes out a powerful, practical cadriciel, which can potentially work on most platforms. Kivy is pleasant to use, and has a very active and available community.

This Cadriciel deserves to dwell on it, and is a serious option if you want to develop a project quickly for one or more platforms.

A story note

This dispatch was started on October 04, 2018 at 2:19 p.m. (on the first of the Pycon FR 2018, in Lille, where part of the Kivy team was present). I believe that it was high the dispatch that remained the longest in the editorial . Like what, everything comes in time to who knows how to wait, as the other would say.

Thank you to all the people who participated in the correction of the dispatch.

Go further

-

-

-
PREV Google Pixel 8 Pro loses more than 200 euros during French Days Rakuten
NEXT 1 month after its release The ultra Poco F7 already sees its price fall on Aliexpress