Kivy GUI in Python

                                            


Introduction 

It's a lot of fun and satisfying to make Kivy apps. This guide should serve as an excellent starting point for getting you started with app development. To follow along with this tutorial, you'll need a basic understanding of Python. 

Kivy allows you to design apps that operate on the following platforms:

  • macOS, Linux, and Windows operating systems.
  • iPad, iPhone iOS devices. 
  • Tablets phones Android devices.
  • UIO is supported by any other touch-enabled professional/homebrew devices (Tangible User Interface Objects).

Kivy gives you the ability to create your code once and have it run on several platforms.

What is Kivy?

Kivy is a Python graphical user interface (GUI) development platform that is open-source. It enables us to create NUI (Natural User Interface) mobile applications and multi-touch application software. It allows developers to create an application once and utilize it across all devices. It can also use mobile APIs to manipulate things like a phone's camera, GPS tracking, and vibrator, among other things. It includes a number of components for building an application, including:

  • OpenGL ES 2 is a graphics library.
  • A wide variety of multi-touch widgets are available.
  • A Kv language for creating custom widgets at an intermediate level.
  • A large number of input devices, such as a mouse, keyboard, and TUIO, are supported, as well as OS-specific multi-touch events.

Why Kivy?

Kivy is a cross-platform framework. As a result, you may use the same code for both your Android and iOS apps. So you just have to code once, and you can use the same code in both your Android and iOS apps. Kivy speeds up and lowers the cost of coding. 

Installing Kivy

The simplest way to install Kivy is through pip, which installs it from the source or from a pre-compiled wheel if one is available.

On Windows, macOS, Linux, and the Raspberry Pi, Kivy provides pre-compiled wheels for the supporting Python versions.  

Setup terminal and pip

Python and pip must be installed before Kivy can be installed. Then open a new terminal with Python installed. Update pip and other installation dependencies in the terminal to the current version (Linux users may need to use python3 instead of python and also use the --user flag in the following commands outside the virtual environment):
        
    python -m pip install --upgrade pip setuptools virtualenv

Create virtual environment

1. In your current directory, create the virtual environment kivy_venv:       
                     python -m virtualenv kivy_venv
2. Make the virtual environment active. Every time you start a new terminal, you'll have to repeat this step from the current directory. This configures the environment to use the new kivy_venv Python.
  •   For Windows CMD, type in the command line:
                 kivy_nenv\Scripts\activate

  •   If it happened that you are in bash terminal on Windows, rather do:
                source kivy_venv/Scripts/activate

  •  If you are in Linux or macOS, rather do:
                source kivy_venv/bin/activate

The route in your terminal should now start with (kivy_venv), indicating that the kivy_venv environment is active. If it doesn't say that, the virtual environment isn't active, and the steps below will fail.


Install Kivy

After finishing the above steps, now we are ready to install Kivy using one of the following options:

Pre-compiled Wheels

Installing the latest stable version of kivy and, if desired, kivy examples from the kivy-team provided PyPi wheels is the simplest approach. Simply follow these steps:

    python -m pip install "kivy[base]" kivy_examples

Kivy's minimum dependencies are also installed as a result of this. Install kivy[base,media] or kivy[full] if you want audio/video functionality in Kivy. The selectors are listed in Kivy's dependencies.

From source

Kivy can be installed from the source with a few extra steps if a wheel isn't available or isn't working. Kivy will be installed from source code and compiled directly on your machine if you want to install it from the source.
Install the additional system dependencies for each platform indicated above: Windows, macOS, Linux.

You can now install Kivy into the virtual environment after installing the dependencies.
To install the stable version of Kivy, run the following commands from the terminal:

        python -m pip install "kivy[base]" kivy_examples --no-binary kivy




   First App

In the video link below I will guide you through how to install Kivy on Windows. In my case, I will be using pip to install Kivy.   
Check out this link for the whole tutorial on Youtube. 


The App will be displaying Hello greet and the user enters a name + click the button and the app will display hello +{name}. the code for the  App is shown below:

First, we will be importing widgets from kivy.uix. Our App will have:
  •  gridLayout  
  • Label
  • Image 
  • Button
  • TextInput 
In Kivy applications, there must be a class and our class is named HelloThere with a build and run method.  the build method should have a return.

from re import MULTILINE
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput


class HelloThere(App):
    def build(self):
        self.window = GridLayout()
        self.window.cols = 1 #number of column the grid will has
        self.window.size_hint = (0.4, 0.3 )
        self.window.pos_hint = {"center_x": 0.5, "center_y":0.5}
       
        self.window.add_widget(Image(source="logo.png"))  # image widget
       
        #label widget
        self.greenting = Label(
                            text="What's your name?",
                            font_size= 18,
                            color= '#00FFCE'
                               )
       
       
        self.window.add_widget(self.greenting)
       
        # text input widget
        self.user= TextInput(
                            multiline=False,
                            padding_y =(20,20),
                            size_hint =(1, 0.5)
                            )
        self.window.add_widget(self.user)
       
        #button widget
        self.button = Button(
                            text="GREET",
                            size_hint = (1, 0.5),
                            bold = True,
                            background_color= '#00FFCE'
                           
                            )

        self.button.bind(on_press=self.callback)
        self.window.add_widget(self.button)
       
        return self.window
         
     
     
     
    def callback(self, inistance):
        self.greenting.text = "Hello " + self.user.text + "!"
       
       

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

References: 


Author: Fatima Zahra Hamdi 


Comments

Popular Posts