In our previous Python tutorial, we have explained to Get Wifi Passwords using Python. In this tutorial, we will explain how to Build a Mobile Application with Python.
In recent web development transformation, the applcation developed to run on all plateform such IOS, Android, Windows, Linux etc and also on all devices (Desktops, Tablets, mobiles etc.).
You can easily develop applications to run on all plateform and devices using Kivy. Kivy is a free and open source Python framework for developing mobile apps and other multitouch application software with a natural user interface.
Here in this tutorial, we will develop a simple calculator app using Kivy.
We need to create following files to develop a calculator app using Kivy:
- main.py: Python file to create App Class.
- calculator.kv: Kivy file to create inteface and add functionality.
So let’s start coding.
Install Required Modules
First we need to install the Kivy module to get started. Use the command below to install the module.
pip install kivy
Import Modules and App Configuration
We will create main.py and import Kivy, GridLayout and other required library.
import kivy from kivy.app import App kivy.require('1.9.0') from kivy.uix.gridlayout import GridLayout from kivy.config import Config
we will also import config to configure and adjust the window size.
Config.set('graphics', 'resizable', 1)
Create Layout Class
We will create layout class CalculatorGridLayout and define Calculate function in it.
class CalculatorGridLayout(GridLayout): def calculate(self, calculation): if calculation: try: self.display.text = str(eval(calculation)) except Exception: self.display.text = "Error"
Create App Class
Now we will create our app class CalculatorApp and call CalculatorGridLayout() layout function to call calculator.kv file to display calculator layout and handle functionality.
class CalculatorApp(App): def build(self): return CalculatorGridLayout()
We will create instance of the class to run the application.
calApp = CalculatorApp() calApp.run()
Here is complete code of our main.py application file.
import kivy from kivy.app import App kivy.require('1.9.0') from kivy.uix.gridlayout import GridLayout from kivy.config import Config Config.set('graphics', 'resizable', 1) class CalculatorGridLayout(GridLayout): def calculate(self, calculation): if calculation: try: self.display.text = str(eval(calculation)) except Exception: self.display.text = "Error" class CalculatorApp(App): def build(self): return CalculatorGridLayout() calApp = CalculatorApp() calApp.run()
Create Layout File and Add Functionality
We will create calculator.kv file same app class and create interface for our calculator with styles and buttons. Then we will add functionality to calculator buttons.
<CustButton@Button>: font_size: 32 <CalculatorGridLayout>: id: calculator display: entry rows: 6 padding: 10 spacing: 10 BoxLayout: TextInput: id: entry font_size: 32 multiline: False BoxLayout: spacing: 10 CustButton: text: "7" on_press: entry.text += self.text CustButton: text: "8" on_press: entry.text += self.text CustButton: text: "9" on_press: entry.text += self.text CustButton: text: "+" on_press: entry.text += self.text BoxLayout: spacing: 10 CustButton: text: "4" on_press: entry.text += self.text CustButton: text: "5" on_press: entry.text += self.text CustButton: text: "6" on_press: entry.text += self.text CustButton: text: "-" on_press: entry.text += self.text BoxLayout: spacing: 10 CustButton: text: "1" on_press: entry.text += self.text CustButton: text: "2" on_press: entry.text += self.text CustButton: text: "3" on_press: entry.text += self.text CustButton: text: "*" on_press: entry.text += self.text BoxLayout: spacing: 10 CustButton: text: "AC" on_press: entry.text = "" CustButton: text: "0" on_press: entry.text += self.text CustButton: text: "=" on_press: calculator.calculate(entry.text) CustButton: text: "/" on_press: entry.text += self.text BoxLayout: CustButton: font_size: 20 text: "My Scientific calculator" on_press: entry.text = ""
Run Application
Finaly run our application through terminal using below command.
python main.py
Ouput: