What is kivy

Kivy is a free Python framework for building applications.It is based on the Model-View-Controller and User Interface Markup Language to define the interface of the application



Some of the features of Kivy

  • we can use kivy to create applications for windows,linux, and Android
  • Kivy has built-in support for multi touch input
  • Kivy uses OpenGL graphics to provide high performance and smooth animations
  • Kivy provides widgets for creating buttons,text input fields,labels,dropdown menus and more
  • Kivy can be easily integrated with other Python libraries such as NumPy and OpenCV



Kivy offers many advantages however there are still  some disadvantages 

  • Kivy has a unique  language called user interface markup language to define the application interface. This can make it more challenging to get started with the framework,specially those who are new to Python or GUI programming.
  • While Kivy provides high performance graphics,it can have performance issues in older or less powerful devices.
  • Even Kivy has a dedicated community of developers and users,it is still hard to finding help and support for specific issues compared to more popular frameworks.
  • Kivy can be integrated with other Python libraries, still its ecosystem of third party libraries and plugins is not user friendly
  • Kivy not have native support for all features of each platform. Developers need to write additional platform specific code to implement certain features.



How to install kivy in linux


  1. Update package manger
    sudo apt-get update

     

  2. Install necessary dependencies using apt
    sudo apt-get install python3-pip python3-dev libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-ttf-dev libportmidi-dev libswscale-dev libavformat-dev libavcodec-dev zlib1g-dev

     

  3. Install Kivy using pip
    sudo pip3 install kivy

     

  4. Verify the installation 
    python3 -c "import kivy"


building a sample calculator using kivy



paste below code as calculator.py and run python calculator.py


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

class Calculator(GridLayout):
    def __init__(self, **kwargs):
        super(Calculator, self).__init__(**kwargs)
        self.cols = 4

        # Create the input box
        self.input = Button(text="0")
        self.add_widget(self.input)

        # Create the clear button
        self.clear = Button(text="C")
        self.clear.bind(on_press=self.clear_input)
        self.add_widget(self.clear)

        # Create the operation buttons
        self.add_widget(Button(text="+"))
        self.add_widget(Button(text="-"))
        self.add_widget(Button(text="*"))
        self.add_widget(Button(text="/"))

        # Create the number buttons
        for i in range(1, 10):
            btn = Button(text=str(i))
            btn.bind(on_press=self.update_input)
            self.add_widget(btn)

        # Create the zero button
        zero = Button(text="0")
        zero.bind(on_press=self.update_input)
        self.add_widget(zero)

        # Create the equals button
        equals = Button(text="=")
        equals.bind(on_press=self.calculate)
        self.add_widget(equals)

    def update_input(self, instance):
        if self.input.text == "0":
            self.input.text = ""
        self.input.text += instance.text

    def clear_input(self, instance):
        self.input.text = "0"

    def calculate(self, instance):
        try:
            self.input.text = str(eval(self.input.text))
        except:
            self.input.text = "Error"

class CalculatorApp(App):
    def build(self):
        return Calculator()

if __name__ == "__main__":
    CalculatorApp().run()