How to make calculator using kivy | Python
Kivy is a platform-independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications.
???????? Kivy Tutorial – Learn Kivy with Examples.
In this article, we will learn how to make a simple calculator using Kivy.
Prerequisites:
1) Basic knowledge of Mathematics
2) Python
3) Kivy
4) Widgets and code understanding of kivy
Basic approach to make A calculator:
1) import kivy
2) import kivyApp
3) import Gridlayout
4) import config(to configure/adjust the window size)
5) Set minimum version(optional)
6) Create Layout class :
define Calculator function in it
: In this i am using try-catch because if any arithmetic
exception occur it will through the error
7) create App class
8) create .kv file (name same as the app class):
1) create buttons
2) Add the style to the buttons
3) Add functionalities of the button
9) return Layout/widget/Class(according to requirement)
10) Run an instance of the class
Implementation of the Approach:
main.py
Python3
import kivy
from kivy.app import App
kivy.require( '1.9.0' )
from kivy.uix.gridlayout import GridLayout
from kivy.config import Config
Config. set ( 'graphics' , 'resizable' , 1 )
class CalcGridLayout(GridLayout):
def calculate( self , calculation):
if calculation:
try :
self .display.text = str ( eval (calculation))
except Exception:
self .display.text = "Error"
class CalculatorApp(App):
def build( self ):
return CalcGridLayout()
calcApp = CalculatorApp()
calcApp.run()
|
calculator.kv
Python3
<CustButton@Button>:
font_size: 32
<CalcGridLayout>:
id : calculator
display: entry
rows: 6
padding: 10
spacing: 10
BoxLayout:
TextInput:
id : entry
font_size: 32
multiline: False
BoxLayout:
spacing: 10
CustButton:
text: "7"
on_press: entry.text + = self .text
CustButton:
text: "8"
on_press: entry.text + = self .text
CustButton:
text: "9"
on_press: entry.text + = self .text
CustButton:
text: "+"
on_press: entry.text + = self .text
BoxLayout:
spacing: 10
CustButton:
text: "4"
on_press: entry.text + = self .text
CustButton:
text: "5"
on_press: entry.text + = self .text
CustButton:
text: "6"
on_press: entry.text + = self .text
CustButton:
text: "-"
on_press: entry.text + = self .text
BoxLayout:
spacing: 10
CustButton:
text: "1"
on_press: entry.text + = self .text
CustButton:
text: "2"
on_press: entry.text + = self .text
CustButton:
text: "3"
on_press: entry.text + = self .text
CustButton:
text: "*"
on_press: entry.text + = self .text
BoxLayout:
spacing: 10
CustButton:
text: "AC"
on_press: entry.text = ""
CustButton:
text: "0"
on_press: entry.text + = self .text
CustButton:
text: "="
on_press: calculator.calculate(entry.text)
CustButton:
text: "/"
on_press: entry.text + = self .text
BoxLayout:
CustButton:
font_size: 20
text: "Scientific calculator"
on_press: entry.text = ""
|
Output:
Last Updated :
17 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...