In this age of mobiles, rapid mobile application development has got a lot of traction. Moreover, app developers are high in demand because of the increase in digitization.
Generally, Python is not used to develop android apps. Usually, the android studio is used as IDE for creating android apps. But Python is a very versatile language. It has also made inroads into android development. Python doesn't have inbuilt functionality to develop mobile apps. But it provides packages and libraries like Kivy, Beeware's Toga library, etc., to create mobile applications.
Such packages and libraries have enabled developers to develop mobile apps in Python. Mobile / Desktop simple applications developed by Kivy library in Python can run on different platforms like android, iOS, windows, etc. Also, the application created using Kivy also gives the same look and feel across these platforms.
With the use of Kivy, you can leverage the simple syntax of Python to develop android applications. But, to use all these and harness the power of Python, first, you need to have your fundamentals clear. Think of any programming language as a tool to solve problems. The most important thing here is how sound is your logic in developing the app or in programming in general.
Let us deep-dive into the kivy library, which helps us develop mobile applications using the Python Programming Language.
Setting up kivy for the Development of Applications.
We need to understand the term when it comes to developing applications that are components. Components mean the widgets that help us assist the end-user app when using it; we have to consider making them perfectly alright to give the user a seamless experience. The components include elements like images, text, buttons, and labels which makes the application interactive for the end-user.
- Good understanding and hands-on experience in Python Programming
- Setting up the Project
Use the below command to install the Kivy library into your respective systems.
Pip install Kivy
In this article, as an example of understanding and learning to build apps in Python, we will try to create a calculator app. Create a folder and rename it to the Calculator app; this will also be along with the folder name also the name of the application that we are trying to build. Create a file and rename it to main.py; add the code given below.
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.text input import TextInput
We will now code the widgets for our calculator application
Create a window object by initializing the oops concept class and name it to AgeCalculator
class AgeCalculator(App): def build(self): self.window = GridLayout() return self.window #code to run as soon as the program executes If __name__==”__main__” AgeCalculator().run()
We are trying to build an app that will return the user's age when they enter their date of birth, so we will now write the code to take the input from the user.
self.ageRequest = Label(text = "Enter your year of birth...") self.window.add_widget(self.ageRequest) self.date = TextInput(multiline=False) self.window.add_widget(self.date)
We have written code to make the app user understand that they must input their birth data to know their age. Similarly, we will now write a simple code to collect the user's data.
self.date = TextInput()
Let us now try to bind the function that can calculate the age and the input given to us by the end-user and the data we have collected from the user, which builds up the complete application.
self.button = Button(text = "Calculate Age") self.button.bind(on_press = self.getAge) self.window.add_widget(self.button)
Let us now write the final function code, the most important one that is the logic calculating the year, date, and month and returns the age for the end-user, with a prompt saying you are x years old.
def getAge(self, event): today = datetime.today().year dob = self.date.text age = int(today) - int(dob) self.ageRequest.text = "You are " + str(int(age)) + " years old"
Running our application
To run the app, we have just built, open the command prompt, go to the project directory, and write the code below to execute it.
We have just built a basic app using the kivy library; you can learn more about the library, and style by formatting a good interface, to give a good user experience.