In this tutorial, we will be creating a basic calculator app using Python and the Kivy framework. Kivy is a Python library for developing multi-touch applications that run on a variety of platforms, including Android and iOS. By the end of this tutorial, you will have a working calculator app that can perform addition, subtraction, multiplication, and division.
Step 1: Installing Kivy
Before we start working on our calculator app, we need to install Kivy. You can install Kivy using pip by running the following command:
pip install kivy
Step 2: Setting up the Project
Create a new directory for your project and navigate to it in the terminal. Inside the project directory, create a new Python file called main.py
where we will write the code for our calculator app.
Step 3: Creating the User Interface
We will use Kivy’s built-in widgets to create the user interface for our calculator app. Open main.py
in your preferred text editor and add the following code:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
class CalculatorApp(App):
def build(self):
layout = BoxLayout(orientation='vertical')
self.display = TextInput(multiline=False, readonly=True, font_size=50)
layout.add_widget(self.display)
buttons = [
['7', '8', '9', '/'],
['4', '5', '6', 'x'],
['1', '2', '3', '-'],
['.', '0', '=', '+']
]
for row in buttons:
h_layout = BoxLayout()
for button_text in row:
button = Button(text=button_text, on_press=self.on_button_press)
h_layout.add_widget(button)
layout.add_widget(h_layout)
return layout
def on_button_press(self, instance):
if instance.text == '=':
try:
self.display.text = str(eval(self.display.text))
except ZeroDivisionError:
self.display.text = 'Error'
else:
self.display.text += instance.text
if __name__ == '__main__':
CalculatorApp().run()
This code defines a CalculatorApp
class that extends the App
class from Kivy. We create a user interface with a TextInput
widget for displaying the input and output, and a grid of Button
widgets for the calculator buttons. We define a method on_button_press
to handle button clicks and perform the calculation when the equals button is pressed.
Step 4: Running the App
To run the app, open a terminal and navigate to the project directory. Run the following command:
python main.py
This will launch the calculator app, where you can input numbers and perform calculations using the buttons.
Step 5: Building the Android App
To build the calculator app for Android, we can use the Buildozer tool, which compiles the Python code into an Android APK file. First, install buildozer using pip:
pip install buildozer
Next, create a buildozer.spec
file in the project directory with the following configuration:
[app]
title = CalculatorApp
package.name = com.example.calcapp
package.domain = org.calcapp
source.include_exts = py,png,jpg,kv,atlas
source.include_patterns = images/*,kivy/*,*.py
requirements = kivy
orientation = portrait
osx.python_version = 3
android.permissions = INTERNET
version = 1.0.0
[buildozer]
log_level = 2
warn_on_root = 1
Finally, run the following command to build the Android APK file:
buildozer -v android debug
After the build is complete, you can find the APK file in the bin
directory of your project. You can install the APK on an Android device and test the calculator app.
Conclusion
In this tutorial, we created a basic calculator app using Python and the Kivy framework. We configured the user interface with text input and buttons for performing calculations, and built the Android app using the Buildozer tool. You can further customize the app by adding more functionality and styling to enhance the user experience. I hope you found this tutorial helpful in creating your own calculator app. Thank you for reading!