Skip to main content

How to make calculator in python kivy

  How make calculator in python We make a calculator in python using kivy module . Kivy module helps us to make android apps . First wait , If you run a kivy program in your terminal then you see a error like this To solve this problem you have to go kivy docs and follow the instruction of kivy docs.  First we have to make a virtual environment and then in the virtual environment you have to install kivy . After that you have to write  kivy_venv\Scripts\activate in your terminal and then run the file . If this method not work then write the comment below. LETS START CODING First we import kivy .For this we hve to write  from  kivy.app  import  App from  kivy.uix.label  import  Label from  kivy.uix.button  import  Button from  kivy.core.window  import  Window from  kivy.uix.gridlayout  import  GridLayout from  kivy.uix.widget  import  Widget from  kivy.lang  import ...

How to make calculator in python kivy

 How make calculator in python

We make a calculator in python using kivy module . Kivy module helps us to make android apps .

First wait , If you run a kivy program in your terminal then you see a error like this








To solve this problem you have to go kivy docs and follow the instruction of kivy docs. 

First we have to make a virtual environment and then in the virtual environment you have to install kivy . After that you have to write kivy_venv\Scripts\activate in your terminal and then run the file . If this method not work then write the comment below.

LETS START CODING

First we import kivy .For this we hve to write 

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget
from kivy.lang import Builder

We can't import kivy directly . So, we import these classes which help in making calculator.

After this  we have to make CalculatorApp(App) class inherit from App and run the program to check .

After this we have to make another CalculatorGrid(Widget) class which is inherit from Widget and call the class in the CalculatorApp(App)

After this we have to make a function in CalculatorGrid(Widget) . For this we have to write 

def calculate(self,calculation):
        if calculation:
            try:
                self.input.text = str(eval(calculation))
            except:
                self.input.text = "Error"


Now we design the calculator in kv language. kv language is a design language like css 

We make claculator.kv file where we design the calculator . For this we have to write

 <CalculatorGrid>

    input:input
    GridLayout:
        size:root.width,root.height
        cols:1
        rows:6
        BoxLayout:          
            TextInput:
                id:input
                font_size:50
                multiline:False
        BoxLayout:
            Button:
                text:"AC"
                on_press:input.text = ""     
           
        BoxLayout:
            Button:
                text:"9"
                id:nine          
                on_press:input.text+=self.text           
            Button:
                text:"8"
                id:three                          
                on_press:input.text+=self.text
            Button:
                text:"7"
                id:four                         
                on_press:input.text+=self.text
            Button:
                text:"/"
                id:divide            
                on_press:input.text+=self.text
        BoxLayout:
            Button:
                text:"6"
                id:six            
                on_press:input.text+=self.text
            Button:
                text:"5"
                id:five            
                on_press:input.text+=self.text
            Button:
                text:"4"
                id:four            
                on_press:input.text+=self.text
            Button:
                text:"*"
                id:multiple            
                on_press:input.text+=self.text
        BoxLayout:
            Button:
                text:"3"
                id:three            
                on_press:input.text+=self.text
            Button:
                text:"2"
                id:two            
                on_press:input.text+=self.text
            Button:
                text:"1"
                id:one            
                on_press:input.text+=self.text
            Button:
                text:"-"
                id:sub            
                on_press:input.text+=self.text
        BoxLayout:
            Button:
                text:"00"
                id:twozero            
                on_press:input.text+=self.text
            Button:
                text:"0"
                id:zero            
                on_press:input.text+=self.text
            Button:
                text:"="
                id:equal
                on_press:root.calculate(input.text)
            Button:
                text:"+"
                id:add        
                on_press:input.text+=self.text
                    

Now we make a object of  CalculatorApp() . For this we write CalculatorApp().run()




HERE ARE THE CODE

claculator.py



claculator.kv



OUTPUT