Python and Kivy Calculator

Posted by


In this tutorial, we will walk through the process of creating a simple calculator application using Python and Kivy. Kivy is a Python library for developing multi-touch applications on multiple platforms, including Windows, macOS, Linux, iOS, and Android. It makes it easy to create user interfaces for applications using a variety of widgets and layout options.

To get started, make sure you have Python installed on your computer. You can download and install Python from the official website (https://www.python.org/downloads/). Once Python is installed, you can install Kivy using pip, the package installer for Python. Open a terminal or command prompt and enter the following command:

pip install kivy

Now that you have Kivy installed, let’s create a simple calculator application. We will use Kivy’s built-in widgets and layout options to create a functional calculator with basic arithmetic operations. Create a new Python file and import the required modules:

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label

Next, create a class for the calculator application that inherits from the App class:

class CalculatorApp(App):
    def build(self):
        layout = GridLayout(cols=4)
        self.display = Label(text='0', font_size=50)
        layout.add_widget(self.display)
        buttons = [
            '7', '8', '9', '/',
            '4', '5', '6', '*',
            '1', '2', '3', '-',
            'C', '0', '=', '+'
        ]

        for button in buttons:
            if button == '=':
                btn = Button(text=button, pos=(0, 0))
                btn.bind(on_press=self.calculate)
            elif button == 'C':
                btn = Button(text=button, pos=(0, 0))
                btn.bind(on_press=self.clear)
            else:
                btn = Button(text=button)
                btn.bind(on_press=self.on_button_press)

            layout.add_widget(btn)

        return layout

In the build method, we create a GridLayout with 4 columns to arrange the display and buttons in a grid layout. We create a Label widget to display the calculator input and output. We define a list of buttons with numbers, arithmetic operators, and a clear button. We iterate over the buttons list and create Button widgets for each button, binding the appropriate event handlers for each button.

Next, we define the event handlers for the button presses:

    def on_button_press(self, instance):
        current = self.display.text
        button_text = instance.text

        if current == '0':
            self.display.text = ''

        self.display.text = current + button_text

    def calculate(self, instance):
        try:
            self.display.text = str(eval(self.display.text))
        except Exception:
            self.display.text = 'Error'

    def clear(self, instance):
        self.display.text = '0'

The on_button_press method appends the button text to the current display text. The calculate method evaluates the expression in the display text using Python’s eval function and updates the display with the result. The clear method sets the display text to ‘0’.

Finally, create and run the application:

if __name__ == '__main__':
    app = CalculatorApp()
    app.run()

Save the file and run it using the Python interpreter. You should see a simple calculator application with a display and buttons for numbers and arithmetic operations. You can enter arithmetic expressions and evaluate them by pressing the ‘=’ button. The ‘C’ button clears the display.

This tutorial covered the basic steps to create a calculator application using Python and Kivy. You can customize the layout, styling, and functionality of the calculator to suit your needs. Kivy offers a wide range of widgets and layout options to create rich user interfaces for various applications. Happy coding!

0 0 votes
Article Rating

Leave a Reply

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@fierrogutierrezmariodariel5234
2 hours ago

I made a very similar calculator for a school project, It was able to graph, calculate derivatives, integrals and take non-elementary integrals, basically a computer algebra system with Sympy library and a bunch of personalized algorithms. But is deprecated now. I recommend you learning a little about web design which is also great for both learning and a good-looking apps design. I am now working in one of those with the Django Framework, you'll learn a lot!

1
0
Would love your thoughts, please comment.x
()
x