Future Value Tutorial  screenshot

Future Value Tutorial

Most of newbies in the world of programming often get confused with what programming language they should choose while diving into the spacious programming world. There are many alternatives to choose from but Python is the cool programming language to start with. Because of its easy syntax structure most of newbies prefer python as their startup.

In this post I'm gonna teach you to make a fully functional Python based GUI application which was written in python programming language and use Tkinter toolkit. Tkinter is the python interface to the Tk GUI toolkit which comes with python installation.

This tutorial is for the fresher who've just started to learn python and know the basic syntax like variable, function, loop and condition etc. I assume that you can make the future value calculator in command line. We'll make the Future value Calculator as given in the picture below. After executing this tutorial you'll make a GUI app like this. This type of small app can be integrated as a supportive app while developing enterprise application as well as it is helpful for small business application solution.

As given in the image above we'll have two menus, File menu which contains exit option and Help menu which contains help and about option. As you can see there will be 5 input boxes from which user can input the value and our program will calculate the future value of given amount after pressing (click to calculate FV button.

For your simplification the formula to calculate future value is

 
FV = present value *(1+rate_per_period)period
Where period = no. of time compounded * total no. of year of investment

Now let's head for the code section.

1. first of all we're gonna import the entire module which we're going to use to develop this application.

Now open up python IDLE and type the following code ( sys module is needed for the functionality of quit and exit button , Tkinter is the GUI toolkit, tkMessageBox as box represent that we are importing tk built in Message Box as box.

 
import sys
from Tkinter import*
import tkMessageBox as box

2. Let's make a function that would return the future value of the user inputted values.

 
def future_value():
    present_value = e1.get()
    while present_value=='':
        box.showerror("Input Error", "please enter present value, If you don't input it future value will be zero(0)")
        present_value=0
    present_value = float(present_value)
    annual rate =  e2.get()
    if annual_rate=='':
        box.showinfo("Input Error", "please enter annual interest rate")
        annual_rate=0
    annual_rate = float (annual_rate)
    period_per_year =e3.get()
    if period_per_year=='':
        box.showwarning("Input Error", "If you don't provide any no. of compounding, the interest is compounded for 1 time in a year")
        period_per_year=1
    period_per_year = float(period_per_year)
    years =  e4.get()
    if years=='':
        years=0
    years = float(years)
    days = e5.get()
    if days=='':
        days=0
    days = float (days)
    days = days/365
    years = years+days
    rate_per_period = annual_rate/period_per_year
    period = period_per_year * years
    FV = present_value *(1+rate_per_period)period

(i) Now we've defined the function called "future_value". As we are getting the value of present_value from the entry box provided by Tk toolkit, we named it first entry as e1 and taking the get function to retrieve the value from the input box. But what if users just leave it blank; to make our program more interactive we're goanna use "while" loop. If user doesn't provide any value then we're gonna show the input error message and assign the value of present_value to zero. Here box.showerror is the built in tool in Tk through which we can give the error message to the user. "Input Error" parameter will be showed as a name of message box and the text between another quote will be printed in the frame.

If user input the value then our program will convert its value to float as given in the code snip

present_value = float(present_value)

ii) Now our code will get the value of annual_rate through e2.get(), here e2 is the input box name and get is the function we'll show the error message as elaborated in present_value section(i.e no. i) but here we use " showinfo" function instead of "showerror" function to make you familiarize with different options.

Following the same procedure we're getting the value of period_per_year, years and days. Little trick which you should have noted on days section as we've converted days in year and added the days in year which will calculate the total no. of year in float.

Finally we've arrived to the calculation section where the FV will be calculated as per the formula given in the code.

Note: please carefully indent the code as presented above, otherwise your program doesn't run or may give wrong result. (Python is an indentation sensitive programming language.)

3. Now it's the time to dive into the graphical user interface section.

master = Tk()
master.title('Future value calculator')

Here the first line of code will make the window frame and second line is for printing the title of the program at the top of frame as given in the image above. You can rename it to whatever you want but don't erase "master.title" because here the text between quote is just the parameter.

4. We're going to show the Menu in the frame.

 
#for showing Menu
menubar = Menu(master)
filemenu = Menu(menubar,tearoff=0)
#other menu option goes here
menubar.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="Exit",command=end)
filemenu.add_separator()

The first two line of code we've written to show the menu bar. Third line of code will add the label "File" at menu bar which is styled as per the second line of code and Fourth line will add the "Exit" option inside the File menu, label contains the text to show and command means the function which should be called upon if user clicks on the option. Here we've used the end command and we'll make the end function in our code a bit later. Last line of code is there to separate the different menus in menu bar.

5. Now let's add the Help Menu in our program which contains "Help" and "About" option.

 
#Help menu
helpmenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="Help ", command=help_us)
helpmenu.add_command(label="About", command=about)

The first line of code is necessary to register the menu, second code will add the "Help" menu in menu bar after the "File" menu. As we're putting two options in "Help" menu the third line of code adds "Help" option and call the function "help_us" which we'll write a bit later and fourth line of code adds "About" option and call the function called "about".

6. Now let's write the function for the menu's option.

 
#exit function
def end():
    master.destroy()
#help_us function for help menu
def help_us():
     box.showinfo("Help", "This program can be used to calculate the future value. Present value is the amount which you want to invest or take as a loan, annual interest rate means the interest rate per year (on certain case the interest rate may be daily, on such case you should use compounding period as 365) and compounding period per year means that in how many period do you charge the, if you charge the interest on daily basis for example most co-operative charge 18-20% interest rate on daily basis. On such case the compounding period should be 365 , if you're charged the interst annually then the period should be 1 that you may not want to enter. Years means years you want to use or invest the amount and days may be remaining days.")
 
# About function for about option in Help menu.
def about():
    box.showinfo("About", "This application(future value calculator) is for those who don't know how to calculate future value. Future value is a strong phenomenon in financial decision making. Not only that this small application is for those who doesn't want to mesh up their time by implementing the formulas. This application is developed by Vaghawan Ojha")

(i) The end function will close the program the built in function inside end function "master.destroy()" close the program. We've used this function in File menu for Exit option as written in the code no. 4 "command=end" which calls the function end.

(ii) The help_us function will show the info message on how to use this application which contains the text message. We've called this function in code no.5 for Help option. Upon the click on "Help" option the info box will popup showing the message written between the quote. You are free to edit the text. Next we've written the function called about which will show the info about this software and its developer.

7. Now come to the main window on which we're goanna add the label and entry box.

 
Label(master,text="Enter Present value").grid(row=0,padx=5,pady=5)
Label(master,text="Enter Annaual Interest rate").grid(row=1,padx=5,pady=5)
Label(master,text="Enter compounding Period per year").grid(row=2,padx=5,pady=5)
Label(master,text="Enter years").grid(row=3,padx=5,pady=5)
Label(master,text="Enter days").grid(row=4,padx=5,pady=5)

Button(master,text='click to calculate FV',command=future_value).grid(row=5,column=1,sticky=W,padx=15, pady=15)

closeButton = Button(master, text="Quit",command=end).grid(row=5,column=2,sticky=W, padx=5, pady=5)

Label(master,text='the future value of given amount is Rs:').grid(row=6)

Label(master,text=FV).grid(row=7)

This is the main part of GUI on which we've given the style for the Label text. The first line of code will print "Enter present value" text on the blank frame and grid function will place this text in row 0 and pad it as per the given value. For each label we've increase the row from 0-4 to show the text and entry box in different row. If you don't specify it all the text and boxes will appear on the same row. Thus it's all about styling your application to provide the better user interface.

At last we've added two buttons, upon the click on " click to calculate FV" button our main function "future_value" will be called and quit button call the function "end" to terminate the application.

The last two line of code is for showing the result we've put it on row 6 and the amount in row 7 for clarity.

8. Now let's add the entry box after the text label.

 
e1 = Entry(master)
e2 = Entry(master)
e3 = Entry(master)
e4 = Entry(master)
e5 = Entry(master)
 
#styling the entry boxes
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=2, column=1)
e4.grid(row=3, column=1)
e5.grid(row=4, column=1)

As we've named the entry boxes e1 ,e2 etc in 2 no. code, we are assigning them as a Tk entry box if we don't write the code like "e1 = Entry(master)" then our boxes do not retrieve the value and throw the error. In our second step we've gridded the box in the different rows, as we're providing one entry box in one row so the column is 1 for all input box.

9. Let's finish up our application.

 
master.config(menu=menubar)
master.mainloop()

This two line of code is necessary, first will configure the menu bar and without the code in second line our program doesn't run. Now save your program, Go to Run menu and click on Run Module option and test your fully functional application.

Upon the completion of this tutorial, you are supposed to have the knowledge of GUI programming in python. If you want to run this program in windows then save this file in python as .pyw extension this doesn't show the command line on backend. With this example you can make your own mind blowing application on python programming language.

If you've any quires, please drop in the comment box.