Recurring Deposit Calculator implementation using Python

FD Calculator
Final Amount:
Total Interest:

RD Calculator Implementation using Python

We will try to implement this Recurring Deposit calculator using Python.
To display the contents, we need a GUI interface. Here we will be using Kivy library.

Code Setup

Download and install the latest version of python from Python Website.

Keep your favorite Python editor ready. This can be a basic notepad application available by default for your OS or you can download and install any free editor like VS Code.

For GUI, we will have to use any third-party libraries. Lots of free options are available. For our RD calculator, we will use Kivy.

Next, install Kivy using command pip install kivy[base].
For this, open the command prompt by typing cmd in run window.
Navigate to the folder we want to place our code by typing the path like cd C:\Users\RD-Calculator
Then run the above installation command.
Kivy installations may fail some times because of incompatibility with latest version of Python.
If any such error occurs, use the command py -3.9 -m pip install kivy[base] (for windows) or python3.9 -m pip install kivy[base] based on the os.

Install Kivy

If you wish to get a quick tutorial of Kivy before starting with the sample. Please have a look at the Kivy Quick Reference.

Then create a file named rd-calculator.py in the same folder and copy the below codes step by step or use the complete code at the end.
To run the code type python rd-calculator.py in the command prompt.

  1. Import kivy to your module

    Import kivy and App modules, which are musts for any kivy application.
    Also import required controls for our application.

    Copied
    import kivy
    kivy.require('2.0.0') # replace with current version !
    
    from kivy.app import App
    from kivy.uix.button import Button
    from kivy.uix.label import Label
    from kivy.uix.gridlayout import GridLayout
    from kivy.core.window import Window
    from kivy.uix.textinput import TextInput
    
  2. Create main window

    Following code creates main window, sets title and size.
    We should have a class that inherits App module which is the startup class.
    def build(self) can be considered as the page_load function. Everything that should happen on page load should come inside this.

    Copied
    class RDCalculator(App):
    
        def build(self):
            self.title = 'RD Calculator'
            Window.clearcolor = (1, 1, 1, 1)#White
            Window.size = (500, 500)
    
    if __name__ == '__main__':
        RDCalculator().run()
    
  3. Create labels and textboxes

    Kivy has different types of basic controls. Styling of controls can be done in 3 ways. In first label, styling is done along with the text as attributes, Second label has styles as comma separated values. Or we can keep the styles as a separate file and write the styles using Kv Language. Refer kivy basics quick tutorial for any doubts.

    Copied
    lblP = Label(text='[halign=left][b][color=000000]Principal Amount[/color][/b][/halign]',
    markup = True)
    lblI = Label(text="Interest Rate per Year", color =(0, 0, 0, 1),
    font_size ="15sp", size_hint=(1.0, 1.0))
    lblM = Label(text='[halign=left][b][color=000000]Number of Months[/color][/b][/halign]',
    markup = True)
            
    self.txtP = TextInput(text='')
    self.txtI = TextInput(text='', font_size=12,multiline=False)
    self.txtM= TextInput(text='')
    
    self.lblR = Label(text="", color =(0, 0, 0, 1),
    font_size ="25sp", size_hint=(1.0, 1.0))
    
    btn = Button(text ="Calculate", font_size ="20sp", background_color =(1, 1, 1, 1), 
    size =(32, 32), size_hint =(.2, .2), pos =(300, 250))
    btn.bind(on_press = self.calculate)
    
  4. GridLayout to align the input fields like a table

    Kivy has different types of layouts. Here we are using GridLayout for displaying the controls. A new GridLayout with 2 columns is defined and controls are added.

    Copied
    layout = GridLayout(cols=2, row_default_height=30, row_force_default=True)
    layout.add_widget(lblP)
    layout.add_widget(self.txtP)
    layout.add_widget(lblI)
    layout.add_widget(self.txtI)
    layout.add_widget(lblM)
    layout.add_widget(self.txtM)
    return layout
    
  5. Place a button for calculation

    Place a button, style it and bind calculate function to it. Also add it to the GridLayout.

    Copied
    btn = Button(text ="Calculate", font_size ="20sp", background_color =(1, 1, 1, 1), 
    size =(32, 32), size_hint =(.2, .2), pos =(300, 250))
    btn.bind(on_press = self.calculate)
    
    layout.add_widget(btn)
    layout.add_widget(self.lblR)
    
  6. Implement the calculation logic inside calculate() function

    A for loop is used to find the interest for each installment and add it together to get the final amount.

    Copied
    def calculate(self, instance):
        f = 4
        p = float(self.txtP.text)  
        r = float(self.txtI.text)   
        m = int(self.txtM.text)
        res = 0.0
        for i in range(1, m + 1):
            res += round(p * ((1 + (r/100) / f) ** (f * i / 12)), 2)
            
        self.lblR.text = str(round(res))
        return
    

Final Code

import kivy
kivy.require('2.0.0') # replace with current version !

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.core.window import Window
from kivy.uix.textinput import TextInput

class RDCalculator(App):

    def build(self):
        self.title = 'RD Calculator'
        Window.clearcolor = (1, 1, 1, 1)#White
        Window.size = (500, 500)
       
        
        lblP = Label(text='[halign=left][b][color=000000]Principal Amount[/color][/b][/halign]',
        markup = True)
        lblI = Label(text="Interest Rate per Year", color =(0, 0, 0, 1),
        font_size ="15sp", size_hint=(1.0, 1.0))
        lblM = Label(text='[halign=left][b][color=000000]Number of Months[/color][/b][/halign]',
        markup = True)
        
        self.txtP = TextInput(text='')
        self.txtI = TextInput(text='', font_size=12,multiline=False)
        self.txtM= TextInput(text='')

        self.lblR = Label(text="", color =(0, 0, 0, 1),
        font_size ="25sp", size_hint=(1.0, 1.0))

        btn = Button(text ="Calculate", font_size ="20sp", background_color =(1, 1, 1, 1), 
        size =(32, 32), size_hint =(.2, .2), pos =(300, 250))
        btn.bind(on_press = self.calculate)
              

        layout = GridLayout(cols=2, row_default_height=30, row_force_default=True)
        layout.add_widget(lblP)
        layout.add_widget(self.txtP)
        layout.add_widget(lblI)
        layout.add_widget(self.txtI)
        layout.add_widget(lblM)
        layout.add_widget(self.txtM)
        layout.add_widget(btn)
        layout.add_widget(self.lblR)
        return layout
        
    def calculate(self, instance):
        f = 4
        p = float(self.txtP.text)  
        r = float(self.txtI.text)   
        m = int(self.txtM.text)
        res = 0.0
        for i in range(1, m + 1):
            res += round(p * ((1 + (r/100) / f) ** (f * i / 12)), 2)
        
        self.lblR.text = str(round(res))
        return

if __name__ == '__main__':
    RDCalculator().run()
Absolute Code Works - Python Topics