Hello, in the world of kivy

Yugam sachdeva
TEK Society
Published in
6 min readAug 21, 2020

What is kivy?

kivy is an opensource platform GUI development library for python. It can be run on Windows, Android, ios, OS X, and GNU/Linux.

Based on Python, which is extremely powerful given its library rich nature.
Write code once and use it across all devices.

How to build an application?

Before start building an application, i suggest you to have some basic knowledge of Python and especially OOPs (object-oriented programming system).

Tools required for system-

  1. kivy is written in python and as such, to use kivy you need an existing installation of python. Now ensure you have latest pip, wheel and virtualenv:
python -m pip install --upgrade pip wheel setuptools virtualenv

2. Now install all dependencies according to your OS.

3. Install kivy:

python -n pip install kivy==1.11.1

4. If you want to run a complex python script you can do it in two ways:

First option is that you can use an integrated development environment (IDE) or an advanced text editor. Most of these programs offer the possibility of running your scripts from inside the environment itself. It is common for them to include a Run or Build command, which is usually available from the toolbar or from the main menu.

The second option is you can use a simple text editor to write the script and save that file with .py extension then open your command line shell or terminal and then go to that folder where that python file is saved and run the command:

python example.py

use python or python3 depending on your application and replace example.py with your python file name.

Now we can start building our GUI application.

Let's get Started

from kivy.app import Appclass main(App):
pass

main().run()

So, what we have done in the above code, in first-line we have imported the App class which will be used to generate a screen for your application. Then we have created a main class which is inheriting the App class in it. App class is having a function named run(), using that we will start running our application and a blank screen will be generated as our output.

How to add widgets in our application?

What are widgets?
In simple language, the widget is a part of the GUI that allows user to interface with the application. widget displays the information and invite the user to act in a number of ways.

kivy is also providing us with many widgets to make our application effective and some of that widgets are-

  • Button
  • TextInput
  • Label
    etc.

we will understand adding widgets by adding a button in our application screen.

from kivy.app import App
from kivy.uix.button import Button
class main(App):
def build(self):
self.btn=Button(text=”Hello World”,size_hint=(0.4,0.05),pos_hint={‘x’:0.3,’y’:0.5})
return self.btn

main().run()

what we have changed to add a widget, firstly you need to import the Button class then you have to add a function named “build” in the main class.

Now you need to create an object of Button class and to apply something on the button you need to pass it as a parameter as i have done, i have given that button a text, size, and its position within the screen.
Now we have to return that button by its id.

NOTE: you cannot change the name of function build because the run function will look only for build function in the class.
similarly, you can add any widget to your application screen.

Now most of the people will wonder that :

How to add multiple widgets in a single screen?

To add more than one widgets to your application screen you need to understand the concept of layouts.
Layout is also a kind of widget that is used to add multiple widgets.
kivy is also providing various types of layouts and some of them are-

  • FloatLayout
  • GridLayout
  • PageLayout
    etc.

we will understand the concept of layout by taking an example of FloatLayout. FloatLayout gives you the flexibility to add widget at any position on the screen.

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
class main(App):
def build(self):
self.fl=FloatLayout()
self.btn=Button(text="Hello World",size_hint=(0.4,0.05),pos_hint={'x':0.3,'y':0.5})
self.fl.add_widget(self.btn)
self.btn1=Button(text="kivy",size_hint=(0.4,0.05),pos_hint={'x':0.3,'y':0.6})
self.fl.add_widget(self.btn1)

return self.fl

main().run()

Now what we have done, we have imported FloatLayout class and created its object, then we have created two-button with different position and then with the help of add_widget function (that every layout contains) we added both the buttons to that layout and in last we have returned that Floatlayout by its id.

Similarly, you can use various layouts in your application according to the demand.
Now you all can build a single screen kivy application but some of the application demand for multiple screens.

Now you all will ask:

How to add multiple screens in your application?

Again kivy is providing us with two class ScreenManager and Screen, and with the help of these class, we are able to add multiple screens in our application.

May be code for multi-screen can become a bit complex but i will try to make it easy to understand, let us have a look of it-

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager,Screen

#first screen
class first(FloatLayout):
def _init_(self,args,*kwargs):
super()._init_(args,*kwargs)
self.btn=Button(text="Go to screen2",size_hint=(0.4,0.05),pos_hint={'x':0.3,'y':0.5})
self.add_widget(self.btn)
self.btn.bind(on_press=self.switch)

def switch(self,instance):
s.current="2"
#second screen
class second(FloatLayout):
def _init_(self,args,*kwargs):
super()._init_(args,*kwargs)
self.btn1=Button(text="Go back",size_hint=(0.4,0.05),pos_hint={'x':0.3,'y':0.5})
self.add_widget(self.btn1)
self.btn1.bind(on_press=self.switchback)

def switchback(self,instance):
s.current="1"

#main class
class main(App):
def build(self):
return s

s=ScreenManager()

s1=Screen(name="1")
s1.add_widget(first())

s2=Screen(name="2")
s2.add_widget(second())

s.add_widget(s1)
s.add_widget(s2)

main().run()

so what we have done here we have imported our both the class ScreenManager and Screen. Managing the screen part is at the bottom so first we will understand what we have done at the top side.

You need to create a class for each screen and you can see i have created two class as my application is containing two screens. Now in the first-class i have inherited the FloatLayout class in it and initialize it also then i have created a button in this class and here you will see a new concept that is binding, i have bind that button with a function which will run on pressing of that button. so what that function is doing is that it is switching the screen.

Same is done with second class and in this class function is switching back to the previous screen.

Now from here, ScreenManager role is started, what you all need to do is that create an object of ScreenManager then for every screen you need to create a screen object as i have two screens so i have created two screen objects now add classes to each screen respectively and also add that screens to the ScreenManager using their respective id.

Now in the main class, you have to return the ScreenManager instead of any other widget.
Now you all are able to build a multi-screen application. Also You can add animation effect, images, videos, transition effect, etc.

If you want to explore further or have any kind of problem you can go through its official documentation, link is given below.

Documentation link:

END NOTE :

For more such posts, do follow out our Publication :
https://medium.com/tek-society

Also do clap! It encourages me to write better!

Thank you!

--

--