The Wayback Machine - https://web.archive.org/web/20240916070132/https://www.geeksforgeeks.org/how-to-make-calculator-using-kivy-python/
Open In App

How to make calculator using kivy | Python

Last Updated : 17 Jan, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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




# Program to create a calculator
   
# Program to Show how to create a switch
# import kivy module   
import kivy 
      
# base Class of your App inherits from the App class.   
# app:always refers to the instance of your application  
from kivy.app import App
    
# this restrict the kivy version i.e 
# below this kivy version you cannot 
# use the app or software 
kivy.require('1.9.0')
 
# for making multiple buttons to arranging
# them we are using this
from kivy.uix.gridlayout import GridLayout
 
# for the size of window
from kivy.config import Config
 
# Setting size to resizable
Config.set('graphics', 'resizable', 1)
## Config.set('graphics', 'width', '400')
## Config.set('graphics', 'height', '400')
 
 
# Creating Layout class
class CalcGridLayout(GridLayout):
  
    # Function called when equals is pressed
    def calculate(self, calculation):
        if calculation:
            try:
                # Solve formula and display it in entry
                # which is pointed at by display
                self.display.text = str(eval(calculation))
            except Exception:
                self.display.text = "Error"
  
 # Creating App class
class CalculatorApp(App):
  
    def build(self):
        return CalcGridLayout()
  
# creating object and running it
calcApp = CalculatorApp()
calcApp.run()


  
calculator.kv
 

Python3




# Custom button
<CustButton@Button>:
    font_size: 32
  
# Define id so I can refer to the CalcGridLayout
# class functions
# Display points to the entry widget
<CalcGridLayout>:
    id: calculator
    display: entry
    rows: 6
    padding: 10
    spacing: 10
 
     
  
    # Where input is displayed
    BoxLayout:
        TextInput:
            id: entry
            font_size: 32
            multiline: False
  
    # When buttons are pressed update the entry
    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
  
    # When equals is pressed pass text in the entry
    # to the calculate function
    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: 
 

 



Previous Article
Next Article

Similar Reads

Python | Make a simple window using kivy
Kivy is a platform independent as it can be run on Android, IOS, linux and Windows etc. Kivy provides you the functionality to write the code for once and run it on different platforms. It is basically used to develop the Android application, but it Does not mean that it can not be used on Desktops applications. Use this command To install kivy: pi
5 min read
Python | Create a stopwatch using clock object in kivy using .kv file
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 Desktop applications. Kivy Tutorial - Learn Kivy with Examples. Clock Object: The Clock object allows you to schedule a function call in
6 min read
How to make a kivy label multiline text?
Kivy is an open source software library for the rapid development of applications equipped with novel user interfaces, such as multi-touch apps. Using Kivy on your computer, you can create apps that run on: Desktop computers: OS X, Linux, Windows.IOS devices: iPad, iPhone.Android devices: tablets, phones.Any other touch-enabled professional/home br
3 min read
How to make a Hyperlink in a Button in Kivy?
Kivy is a platform-independent GUI tool in Python. It can run on Android, IOS, Linux and Windows, etc. This is the only GUI library from python which can independently run on the android device even we can use it on Raspberry Pi also. If you are new to kivy you can learn from this link. In this article, we will develop a GUI window using kivy frame
2 min read
Circular (Oval like) button using canvas in kivy (using .kv file)
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.In this article we will going to learn about how can we create a rounded or circular button in kivy using canvas. Y
3 min read
Python | AnchorLayout in Kivy using .kv file
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. &#128073;&#127997; Kivy Tutorial - Learn Kivy with Examples. AnchorLayout: The AnchorLayout aligns its children to
4 min read
Python | StackLayout in Kivy using .kv file
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. StackLayout: To use StackLayout first import StackLayout by bel
3 min read
Python | FloatLayout in Kivy using .kv file
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. &#128073;&#127997; Kivy Tutorial - Learn Kivy with Examples. FloatLayout: Floatlayout provides us the flexibility
3 min read
Python | Relative Layout in Kivy using .kv file
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. Relative Layout: This layout operates in the same way as FloatL
4 min read
Python | PageLayout in Kivy using .kv file
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. PageLayout: The PageLayout works in a different manner from other layout
5 min read
Python | Adding image in Kivy using .kv file
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. Image Widget: The Image widget is used to display an image. To use the
4 min read
Python | Switch widget in Kivy using .kv file
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. Switch widget: The Switch widget is active or inactive, as a me
4 min read
Python | Spinner widget in Kivy using .kv file
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. &#128073;&#127997; Kivy Tutorial - Learn Kivy with Examples. Spinner widget : To work with spinner you must have t
3 min read
Python | Popup widget in Kivy using .kv file
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. &#128073;&#127997; Kivy Tutorial - Learn Kivy with Examples. Popup widget: To use popup you must have to import :
4 min read
Python | ScreenManager in Kivy using .kv file
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. ScreenManager widget : The screen manager is a widget which is used to managing multiple screens for your applicat
6 min read
Python | Canvas in Kivy using .kv file
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 Desktop applications. Kivy Tutorial - Learn Kivy with Examples. Canvas : The Canvas is the root object used for drawing by a Widget. A ki
4 min read
Python | Carousel Widget In Kivy using .kv file
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. Carousel widget: The Carousel widget provides the classic mobi
3 min read
Python | Animation in Kivy using .kv file
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. Animation: Animation and AnimationTransition are used to animate Widget properties. You must specify at least a
3 min read
Python | Progressbar widget in kivy using .kv file
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. &#128073;&#127997; Kivy Tutorial - Learn Kivy with Examples. Progressbar Widget: ProgressBar widget is used to vis
3 min read
Python | Accordion in kivy using .kv file
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. Accordion: The Accordion widget is a form of the menu where the
2 min read
Python | TextInput in kivy using .kv file
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. &#128073;&#127997; Kivy Tutorial - Learn Kivy with Examples. TextInput: The TextInput widget provides a box for ed
3 min read
Python | Drop-down list in kivy using .kv file
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. &#128073;&#127997; Kivy Tutorial - Learn Kivy with Examples. Dropdown list A drop-down list can be used with custo
3 min read
Python | Toggle button in kivy using .kv file
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. Toggle button: The ToggleButton widget acts like a checkbox. Wh
3 min read
Python | Create a stopwatch Using Clock Object in kivy
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 Desktop applications.In this, we are going to see how can we create a stopwatch using a label. In the code, we will be creating just a c
4 min read
Python - Change button color in kivy using .kv file
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. In this article we will learn how to change the background color of button in kivy in .kv file. background_color:
3 min read
Python - Change kivy button size and position using kv file
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.In this article we will see that how can we can change the size and the position of button in kivy python in kv fil
3 min read
Python - Rounding button corners in kivy using .kv file
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.In this article we will going to learn how to round the button corners in kivy using .kv file in Python.Widgets are
4 min read
Login Application and Validating info using Kivy GUI and Pandas in Python
Prerequisites : Kivy, Pandas Kivy is a multiplatform GUI library, known for being responsive. It provides management of multiple screens in a single application. In this application we will be using multiple screens to log in user's info and validate it. We will save the information in a csv file and use pandas to validate the information inside of
5 min read
Clock App with Kivy using Python
In this article, we are going to see how to develop a Clock Application with Kivy using Python. Kivy is a graphical user interface opensource Python library that allows you to develop multi-platform applications on Windows, macOS, Android, iOS, Linux, and Raspberry-Pi. In addition to the regular mouse and keyboard inputs, it also supports multitouc
4 min read
Disable kivy button using .kv file
In this article, we will learn how to disable a button in kivy using .kv file, there are some places where we need to disable the buttons. Let's see how to do that. Kivy Tutorial – Learn Kivy with Examples. The Button is a Label with associated actions that are triggered when the button is pressed (or released after a click/touch). We can add funct
3 min read
Article Tags :
Practice Tags :