Create a simple GUI in Python using Kivy

kivy gui example
Spread the word!

Motivation

I’m going to introduce two very basic sample codes to give a simple overview on the Kivy library. It’s assumed that you have already installed it.

Example #1 – only using a .py file

In this example only a .py file is used to create graphical elements of the Kivy app. I hope this snippet can be a good introduction to understand the basic structure.

Result:

kivy gui example

Example #2 – mixing .py with .kv file

In this case, two buttons already exist because they are initially defined in the .kv file, which describe the layout and the element of the GUI. Further buttons can be dinamically added by clicking on the first button, which will call a function from the .py code. It’s also possible to change the first button name by clicking on the second one.

The Kivy file:

First view of the GUI:

kivy gui example 1

After clicking on “FIRST”:

kivy gui example 1-1

After clicking on “SECOND”:

kivy gui example 1-2

Example #2.1 – Alternative using bind function

Instead of calling the Python function from the .kv file, it’s possible to bind a button click to a function directly from the Python script e.g. with the following line of code:

 

Be the first to comment

Leave a Reply

Your email address will not be published.


*