Learning Kivy with Example: Part I

Kivy is a GUI development Library built with python.

Here I will document what I learned doing the project,

Project: an application that provides 3 buttons and an output area in the same window. adds, subtracts and multiplies on click.

Project requirement:

  • python 3.x.x
  • kivy
  • IDE or code editor

Project Structure

project directory/
- kv/
    - buttons.kv
- main.py
- main.kv

Code Explained:

It all starts in the main.py file. From here we create the flow of the application. It also works as our run class.

main.py should contain 4 core section as the basis

  • the imported packages section
  • A container section to hold the core functionality of the app
  • the MainLoop section to build the app for running
  • a Run section

For the starter: we plan to add 3 buttons and an output area in the app.

Let's Begin . . .

In the main.py file we need to import the packages.

from kivy.app import App
from kivy.uix.button import Button

we import App from kivy.app as all kivy applications are instances of the kivy App class. We will use that to create our own app. We need 3 Buttons that's why we import buttons from the kivy.uix.button . buttons are part of the uix section of kivy.

As we will have 3 buttons and a output section it is a good idea to put them inside a layout system so that we don't have to manually set the position o the buttons. In this case, we will use the GridLayout. Creating the Container with this layout will provide all the properties of the GridLayout to our Container class.

from kivy.uix.gridlayout import GridLayout

Now it is time to start working on the core of our application. Our target is to create a system that can add, subtract and multiply. To do that we must provide at least two values, and 3 functions to independently add, subtract or multiply. All the functionality will go inside our container section.

class Container(GridLayout):

    def add_one(self):
        pass

    def subtract_one(self):
        pass

    def multiply(self):
        pass

for the moment we don't have the value so that we are keeping the functions inactive. Now let's create the buttons instances in the main.py file. NOTE that, each Button will have its own separate class that inherits from the Button we imported at the top.

class AddButton(Button):
    pass

class MultiplyButton(Button):
    pass

class SubtractButton(Button):
    pass

Now comes the Rest of the two Base Section of the app. The MainLoop and The Run section

class MainApp(App):
    def build(self):
        self.title = "My Test APP"
        return Container()

In the MainApp class, we inherit the App class we imported from Kivy. We declare the build function that helps us set the name of the app and returns the Container as The Object that would be run.

if __name__ == '__main__':
    app = MainApp()
    app.run()

in the Run section, we take the instance of MainApp which returns the Container instance. And we call the run function on it. So, in summary, we are instancing the Container class and calling the run() function on that.

Learning Kivy with Example: Part II

Quick Access

Faster way to traverse the blog