Python 3 Kivy Simple Arithmetic Calculator GUI Script Desktop App Full Project For Beginners

You are currently viewing Python 3 Kivy Simple Arithmetic Calculator GUI Script Desktop App Full Project For Beginners

 

Welcome folks today in this blog post we will be building a simple arithmetic calculator in kivy using python. All the full source code of the application is given below.

 

 

Get Started

 

 

In order to get started you need to install the following library using the pip command as shown below

 

pip install kivy

 

After installing this library you need to make an app.py file and copy paste the following code

 

app.py

 

import kivy
kivy.require('1.2.0')
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.clock import Clock
from kivy.properties import ObjectProperty, NumericProperty, ListProperty
from kivy.factory import Factory
from kivy.config import ConfigParser
from kivy.uix.settings import Settings

class Calculator(Widget):
    ti = ObjectProperty(None)
    lastOp = ""
    memorynum = 0.0
    operation = False

    def _execute(self, op):
        if op in "0123456789.":
            if self.lastOp == "=":
                self.lastOp = ""
            self._setText(op)

        elif op in "+-/x^" and self.lastOp != "=":
            if self.lastOp != "":
                self._exeOp(op)
            else:
                self.memorynum = float(self.ti.text)
            self.lastOp = op
            self.operation = True

        elif op in "Cc":
            self.ti.text = ""
            self.memorynum = 0.0
            self.lastOp = ""

        elif op == "<" and self.lastOp != "=":
            self.ti.text = self.ti.text[:-1]

        elif op == "=" and self.lastOp != "=":
            self._exeOp(op)
            self.memorynum = 0.0
            self.lastOp = "="
            self.operation = True

        elif op == "1/2" and self.lastOp != "=":
            self.ti.text = str(float(self.ti.text)/2)

    def _exeOp(self, op):
        if self.lastOp == "+":
            self.memorynum += float(self.ti.text)
        elif self.lastOp == "-":
            self.memorynum -= float(self.ti.text)
        elif self.lastOp == "x":
            self.memorynum *= float(self.ti.text)
        elif self.lastOp == "/":
            self.memorynum /= float(self.ti.text)
        elif self.lastOp == "^":
            self.memorynum = self.memorynum ** float(self.ti.text)
        self.ti.text = str(self.memorynum)

    def _setText(self, op):
        if op == "." and "." in self.ti.text:
            return
        if self.operation:
            self.ti.text = op
            self.operation = False
        else:
            self.ti.text += op

class CalcApp(App):
    use_kivy_settings = False
    def build(self):
        self.Calc = Calculator()
        return self.Calc

    def build_config(self, config):
        config.setdefaults('Calculator Colors', \
            {
            'CC' : 'Off',
            'NB' : 'default',
            'NBT': 'default',
            'OB' : 'default',
            'OBT' : 'default',
            'SB' : 'default',
            'SBT' : 'default',
            'TIB' : 'default',
            'TIF' : 'default'
            })
    def on_config_change(self, config, section, key, value):
        if config is self.config:
            if key == "CC" and value == "On":
                print("fuck")
            else:
                print(section, key, value)
    def build_settings(self, settings):
        settings.add_json_panel('Customization',
                self.config,
                "settings_calc.json")
    def on_pause(self):
        return True
    def on_resume(self):
        pass

if __name__=='__main__':
    CA = CalcApp()
    CA.run()

 

 

Now you need to make another file called calc.kv inside the root directory and copy paste the following code

 

calc.kv

 

 

#:kivy 1.2.0

<CalcButton@Button>:
    the_root: self.parent.my_root
    on_press: self.the_root._execute(self.text)
    background_color: [1, 1, 1, 1]
    color: [1, 1, 1, 1]
    font_size: self.width/2

<CalcButtonOp@CalcButton>:
    background_color: [0.6, 0.6, 0.6, 1]
    color: [0.2, 0.8, 1, 1]

<CalcButtonSpecial@CalcButton>:
    background_color: [0.3, 0.6, 0.6, 1]
    color: [0, 1, 1, 1]

<Calculator>:
    ti: myti
    BoxLayout:
        size: root.width, root.height
        orientation: "vertical"
        spacing: 10
        padding: [5, 5, 5, 5]
        TextInput:
            size_hint: 1, 0.20
            id: myti
            text: ""
            readonly: True
            font_size: 100
            background_color: [1, 1, 1, 1]
            foreground_color: [0, 0, 0, 1]
        GridLayout:
            cols: 4
            rows: 5
            spacing: [8, 8]
            my_root: root
            CalcButtonSpecial:
                text: "^"
            CalcButtonSpecial:
                text: "1/2"
            CalcButtonSpecial:
                text: "<"
            CalcButtonSpecial:
                text: "C"
            CalcButton:
                text: "7"
            CalcButton:
                text: "8"
            CalcButton:
                text: "9"
            CalcButtonOp:
                text: "x"
            CalcButton:
                text: "4"
            CalcButton:
                text: "5"
            CalcButton:
                text: "6"
            CalcButtonOp:
                text: "-"
            CalcButton:
                text: "1"
            CalcButton:
                text: "2"
            CalcButton:
                text: "3"
            CalcButtonOp:
                text: "+"
            CalcButton:
                text: "0"
            CalcButton:
                text: "."
            CalcButtonOp:
                text: "="
            CalcButtonOp:
                text: "/"

 

 

Now if you execute the above python script app.py by typing the below command as shown below

 

python app.py

 

Leave a Reply