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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
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() |
Now you need to make another file called calc.kv
inside the root directory and copy paste the following code
calc.kv
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
#: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) |
Now if you execute the python
script by typing the below command
python main.py