Python Modifying Future Value Program with a Python GUI

  • Thread starter Thread starter gravenewworld
  • Start date Start date
  • Tags Tags
    Coding Gui Python
AI Thread Summary
The discussion revolves around modifying a Python program to create a future value calculator with a graphical user interface (GUI). The user, a beginner in Python, struggles to implement functionality that allows input for principal and annual percentage rate (APR) through entry boxes. Key issues identified include the need to assign values to the variables for principal and APR before using them in calculations. Suggestions are made to switch from the current graphics module to Tkinter, which is recommended for GUI development in Python. The conversation highlights the importance of correctly formatting code and using methods like `getText()` to retrieve user input, which must be converted to a numeric type for calculations. The user expresses gratitude for the guidance, indicating a focus on learning basic GUI and object-oriented programming concepts.
gravenewworld
Messages
1,128
Reaction score
27

Homework Statement



To modify a future value program so that a user inputs the data w/ a GUI


Homework Equations



principal = principal * (1+apr)

The Attempt at a Solution



Anyone familiar w/ python coding? I'm a beginner. I'm absolutely stuck on this problem, I seem to have gotten everything, except I simply can not figure out how to write the program so that it can take the values the user inputs for principal and apr in the entry boxes and uses those values to run the equations for future value over 10 years. Been stuck on this for hours. Here's my code:


from graphics import *

def main():

# Get principal and interest rate in GUI
win = GraphWin("Future value calculator", 300, 300)
win.setCoords(0, 0, 300, 300)
Text(Point(75, 200), " Enter principal: "). draw(win)
Text(Point (75, 150), "Enter interest rate: ").draw(win)
input = Entry(Point(200, 200), 10).draw(win)
input = Entry(Point(200, 150), 10).draw(win)
button = Text(Point(150, 20), "Calculate")
button.draw(win)
Rectangle(Point(100, 10), Point(200, 30)).draw(win)


#mouse click
win.getMouse()


# Create a graphics window with labels on left edge
win = GraphWin("Investment Growth Chart", 320, 240)
win.setBackground("white")
win.setCoords(-1.75,-200, 11.5, 10400)
Text(Point(-1, 0), ' 0.0K').draw(win)
Text(Point(-1, 2500), ' 2.5K').draw(win)
Text(Point(-1, 5000), ' 5.0K').draw(win)
Text(Point(-1, 7500), ' 7.5k').draw(win)
Text(Point(-1, 10000), '10.0K').draw(win)


# Draw bar for initial principal
bar = Rectangle(Point(0, 0), Point(1, principal))
bar.setFill("green")
bar.setWidth(2)
bar.draw(win)

# Draw a bar for each subsequent year

for year in range(1, 11):
principal = principal * (1 + apr)
bar = Rectangle(Point(year, 0), Point(year+1, principal))
bar.setFill("green")
bar.setWidth(2)
bar.draw(win)

input("Press <Enter> to quit.")
win.close()

main()



I'm a beginner and have only been coding for 1.5 weeks, so I need simple explanations with no fancy expert coding tricks.
 
Technology news on Phys.org
Firstly I'm going to suggest you use a different GUI package, TkInter is the standard one used with python, that graphics module isn't that great.

Once I managed to get the code reformatted correctly (always use [noparse]
Code:
[/noparse] tags around any code so that it maintains the correct formatting, especially important for python), I found the problem was you hadn't assigned apr and princpal values before you used them.

Code:
SNIP
    Text(Point(75, 200), " Enter principal: "). draw(win)
    Text(Point (75, 150), "Enter interest rate: ").draw(win)
    input1 = Entry(Point(200, 200), 10)
    input1.draw(win)
    input2 = Entry(Point(200, 150), 10)
    input2.draw(win)
    
    SNIP

    Text(Point(-1, 7500), ' 7.5k').draw(win)
    Text(Point(-1, 10000), '10.0K').draw(win)

    principal = float(input1.getText())
    apr = float(input2.getText())

    # Draw bar for initial principal
    bar = Rectangle(Point(0, 0), Point(1, principal))
    bar.setFill("green")
    bar.setWidth(2)
    bar.draw(win)

   SNIP

That shows what I had to edit (red bits) and where it is in relation to the rest of the code.

Its still given me an error when you try to close it but that should show you what you needed. Although definitely think about trying Tkinter unless this is for a course/class.

input1 = Entry(Point(200, 200), 10) , declares an object of type "Entry" with the specified parameters.

input1.draw(win) , draws the object onto the gui window.

principal = float(input1.getText()) , this sets the value of principal so you don't get the error, the getText() method returns the string that is currently in the Entry object. This then needs to be converted to a numeric type so it will work properly with the rest of the code. I've used float since the value can be a decimal.

Hope this helps abit.
 
Yes, thanks very much. I was trying to follow the textbook and totally missed the error in my code where I had .draw(win) after entry. I think that's what kept messing up. I think we'll learn tkinter later on in the class, we're just learning the very basics of making GUIs and object oriented programming for now.

Thanks again!
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top