GUI programming in python: why I chose to use Kivy
GUI programming is something apart from typical Python coding.
You need to use a specific library. So the immediate question is which one?
Tkinter? PyQt/PySide? wxPython? ...
The list is long and overwhelming.
I tested quite a few and spend (too much) time comparing them.
The result?
If you read the title of this article, you already know the answer: I chose Kivy.
Let me explain why this choice so you can make an informed decision yourself for your project (and the skills you want to add to your dev stack).
GUI programming for python: the jungle
As I said, there is a long list of frameworks and libraries to create GUI in python. The main ones are Tkinter, PyQt/PySide, and wxPython.
Any of these can do the job.
But the effort and the result (both visually and in the code) are quite variable. Starting with the installation.
I was looking for something light, with a modern look and cross-platform. Also, I wanted to be able to develop the GUI on macOS (my computer) to deploy it on Windows 10 (PC for data acquisition) for my current project.
The thing is that none of these really fitted.
I had a long play with Qt.
I got confused between Qt Designer and Qt Creator, dealing with compatibility issues with other libraries. And the look was quite different between platforms in my experience, because it tries to use the native look and feel.
I don't care about that. I don't want to spend days troubleshooting dependencies and versions of each module. On two different OS.
Time is precious.
That's when I saw the light. (no I didn't die of boredom)
Enter Kivy
I saw this name quite a few times on forums, but people were talking about mobile apps development and it is not my goal.
I want to develop a desktop app.
Until I became quite frustrated by the other (many) solutions I checked. I decided to have a look at Kivy.
I am glad I did.
It is awesome!
It doesn't look like that. It doesn't have the big names behind it such as Qt.
But it has the style. The flow.
It is clean, fast and elegant. All you want as a developer.
Enough teasing; let's get more specific.
Kivy is an open-source (yay!) Python library (MIT licenced) for rapid development of applications.
That's a good start.
What else?
It is widely cross-platform, from Linux to Raspberry Pi or Android/iOS (with support for Apple Silicon hardware).
It has excellent features for multi-touch devices (I don't care here but cool).
It is fast. Really fast. Written partly in C using Cython for execution speed and OpenGL for GPU acceleration -> super responsive apps.
The list goes on and on, and the best is to check out this presentation. A bit old but still mostly up-to-date to my knowledge.
Advantages of Kivy
So what are the advantages of learning and using Kivy for your projects?
Drawbacks
A simple code
Let's see the inevitable Hello World using Kivy.
from kivy.app import App
from kivy.uix.button import Button
class TestApp(App):
def build(self):
return Button(text='Hello World')
TestApp().run()
This code is in Python only.
But the cool thing, and one of the best advantages for me, is that you can pretty much separate the UI from the logic. Good programming practice.
The same code using the kv language.
Python code:
from kivy.app import App
class TestApp(App):
pass
TestApp().run()
Kv code: (in a test.kv file)
Button:
text='Hello World'
Easy, right?
You don't even need to import the Button class.
Many developers use Python code, but I think they are missing out on the best of this library.
You can bind widgets directly within your kv file with one-line code.
Try it and you will feel how powerful it is.
Conclusion
If you need a lightweight GUI that you can build quickly with some yummy code, you might want to try Kivy.
Even if you don't want to develop an app or a game.
Have fun,
Francois
Subscribe to my newsletter
Read articles from Francois El-Daher directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by