WRD 204 Instruction Set
by gokulpandiyaraj in Circuits > Computers
422 Views, 0 Favorites, 0 Comments
WRD 204 Instruction Set
Gokulraj Pandiyaraj
The following instructions provide step by step guide for creating an investment calculator in python. using GUI. This instruction set aims to assist people who have intermediate knowledge of python. Import tkinter provides us access to all the code necessary for creating GUI. When creating GUI, you usually just put it inside of a class and underneath has a function called init where you put in self argument to access the classes attributes.
Learning how to code the compound interest rate in python as well as creating an investment calculator by using GUI.
Getting Started
Open the Python module and click on the new file option.
Setting Up
Type import tkinter to get GUI parts running.
Class and Init Function Argument
Make sure to create a class and underneath it type in function def init. Inside the init function argument use self so that you can have access to attributes and methods of the class.
Windows and Frames
After getting your init function setup to add the code shown the image below. This code will create the main window and assignment, add frames to get the GUI window initialized. Have the main window variable will set up and create the GUI screen and create the frames or the boxes you need to have a frame number so that it knows which place to put it in.
Having Variables Investment
Give self. a variable name to set up the buttons for which should be which. Using proper variable names is recommended so that you don't confuse yourself and others when explaining your code. The recommended variables are investmentAmt, year, and annualInterestRate which are used to find the futurevalue. These variables are listed in red in the image below.
Caution: When naming variables, don't use variables names that are predefined or could be confusing. This can either make your code not work or can confuse you which variables are which.
Ex: v = my steps
str = my steps
The first one is an example of an improper variable name. You would have to be more specific of the name rather than just putting a random letter. Though it will work when explaining to someone they will not understand what this certain variable means and what its purpose is. The second one will produce a syntax error since str is a predefined variable which cannot be used to initialize statements or variables.
Adding Frames to Window
Make sure to confirm by adding your frames to your windows so that you don't get a blank screen. You still need to add another function before the window will work.
Calculate Function With Button Click
The new function name can be something like calculate, anything related to investment, then add the investment equation shown in the picture below. You should also take note of the code in the first picture since it has invAmt, years, and annual which use.entry.get() to access it from the GUI portion.
Displaying Window
Add this code so that the window can get displayed on the screen.
Adding a Click Button
Add a clickable button to GUI to display the future value and make sure to refer back to step 7 so that you put the button in your calculate function that was used to store the compound interest formula inside the button code so that the button knows that's the function it should use to display the results.
Using Estimated Values
Normally in the real world, our future value would be presented in exact values. But the numbers just get so long and tedious to keep track so just for this program, we are going to use import math to have access to the methods that round the future value.
Use Math.floor()
To have an estimated value you should use math.floor(futurevalue). This does floor rounding meaning it will round the number DOWNWARDS to the nearest integer.
Ex if the result is 278.956 the estimated value will be 278
Calling on Class
Make sure to have a code like variable = myclass() at the very bottom all the way towards the left which is outside the function so that way it would have access to your whole code in your program.
Final
If you followed the steps correctly your output screen should look like this.
If it does, then congratulations you have successfully created a working investment calculator in python as well as applying that in GUI.
For troubleshooting this you will have to see what exactly the error is in the shell and based on the line number you can use the debugger icon which will run each line. If it stops during the middle rather than at the end, then you have found exactly what code produces the error. The debugger is useful for running the logic portion of the program and this will let the programmer know where exactly the error was. If you have any trouble with variables names refer to step 5 in caution.
This set has provided step by step guidance for programming investment calculator using GUI with Python IDLE software. Good luck and enjoy programming!
If you have any questions about certain steps let me know.