Python 3 Kivy Mini Scientific Advanced Arithmetic Calculator GUI Script Desktop App Full Project For Beginners

Python 3 Kivy Mini Scientific Advanced Arithmetic Calculator GUI Script Desktop App Full Project For Beginners

 

Welcome folks today in this blog post we will be building a mini scientific 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
import math

kivy.require("1.11.1")

from kivy.core.window import Window
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.lang import Builder
from fractions import Fraction


class CalcGridLayout(GridLayout):
    Window.size = (320, 500)

    def fraction(self, j):
        if "=" in j:
            s = j.split("\n=")
            j = (str(eval(s[1])))
            if "." in j:
                self.display.text = s[0] + "\n=" + str(Fraction(float(j)).limit_denominator(1000))
            elif "/" in j:
                self.display.text = j + "\n=" + str(eval(j))

        else:
            self.display.text = j + "\n=" + str(eval(j))

    def sign(self, input):
        if input:
            c = []
            if "=" in input:
                c = input.split("\n=")
            else:
                c.append(0)
                c.append(input)
            try:
                if c[1][0] == "-":
                    self.display.text = c[1][1:]
                else:
                    self.display.text = "-" + c[1]
            except Exception:
                pass

    def calculate(self, calculation):
        g = calculation
        if calculation:
            try:
                if "%" in calculation:
                    c = calculation.split("%")
                    calculation = "/100".join(c)
                if "\u03C0" in calculation:
                    c = calculation.split("\u03C0")
                    calculation = "(22/7)".join(c)
                if "^" in calculation:
                    c = calculation.split("^")
                    calculation = "**".join(c)
                if "\u00B2" in calculation:
                    c = calculation.split("\u00B2")
                    calculation = "**2".join(c)
                if "=" in calculation:
                    s = calculation.split("\n=")
                    self.display.text = (str(eval(s[1])))
                if "=" not in calculation:
                    self.display.text = g + "\n=" + str(eval(calculation))

            except Exception:
                self.display.text = "Error"


class CalculatorApp(App):
    def build(self):
        return CalcGridLayout()


CalcApp = CalculatorApp()
CalcApp.run()

 

See also  Python 3 Tkinter Instagram API Comment on User Profile & Images Bot GUI Desktop App Full Project For Beginners

 

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

 

calc.kv

 

#:import Clock kivy.clock.Clock
<Button@Button>:
    font_size:'25sp'
    size: 10, 10
    padding: 10, 10
ScrollView:
    do_scroll_x: False
    do_scroll_y: True




<CalcGridLayout>:

    id:calculator
    display: entry
    rows:7
    columns:5
    padding:3
    spacing:1
    BoxLayout:

        TextInput:
            id: entry
            size_hint:1,1
            halign: 'right'
            valign: 'bottom'

            cursor_color:(0,0,1,1)
            scroll_y: 1

            font_size:24
            multiline: True


    BoxLayout:

        spacing:1
        Button:
            text:"%"
            on_press:entry.text += self.text
        Button:
            text:"^"
            on_press:entry.text += self.text
        Button:
            text:"/"
            on_press:entry.text+=self.text
        Button:
            text:"del"
            on_press:entry.text = entry.text[:-1]
        Button:
            background_color: (0.1, 0.5, 1, 1)
            text:"AC"
            on_press:entry.text =""
    BoxLayout:

        spacing:1
        Button:
            text:"("
            on_press:entry.text += self.text
        Button:
            text:")"
            on_press:entry.text += self.text
        Button:
            text:'x\u00B2'
            on_press:entry.text+="\u00B2"
        Button:
            text : '\u03C0'
            on_press:entry.text+=self.text
        Button:
            text:"sd"
            on_press:calculator.fraction(entry.text)
    BoxLayout:
        spacing:1
        Button:
            text:"7"
            on_press:entry.text += self.text
        Button:
            text:"8"
            on_press:entry.text += self.text
        Button:
            text:"9"
            on_press:entry.text += self.text
        Button:
            text:"+"
            on_press:entry.text += self.text


    BoxLayout:
        spacing:1
        Button:
            text:"4"
            on_press:entry.text += self.text
        Button:
            text:"5"
            on_press:entry.text += self.text
        Button:
            text:"6"
            on_press:entry.text += self.text
        Button:
            text:"-"
            on_press:entry.text += self.text
    BoxLayout:
        spacing:1
        Button:
            text:"1"
            on_press:entry.text += self.text
        Button:
            text:"2"
            on_press:entry.text += self.text
        Button:
            text:"3"
            on_press:entry.text += self.text
        Button:
            text:"*"
            on_press:entry.text += self.text
    BoxLayout:
        spacing:2
        Button:
            text:"\u00B1"
            on_press:calculator.sign(entry.text)
        Button:
            text:"0"
            on_press:entry.text += self.text
        Button:
            text:"."
            on_press: entry.text+=self.text
        Button:
            text:"="
            background_color: (0.1, 0.5, 0.9, 1)
            on_release:calculator.calculate(entry.text)

 

See also  Python 3 Advanced Email Keylogger Script Which Sends Keystrokes Logs as Email Using Schedule and Pynput Library Full Project For Beginners

 

Now if you execute the python script by typing the below command

 

python main.py

 

Leave a Reply