Modifying Future Value Program with a Python GUI

In summary, the conversation is about modifying a program to use a GUI for user input and calculate future value over 10 years. The code provided is for a graphics window, but the issue is with getting the user input to work. The solution involves using the TkInter GUI package and assigning values for principal and apr before using them in the code.
  • #1
gravenewworld
1,132
26

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
  • #2
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)
    [COLOR="Red"]input1 = Entry(Point(200, 200), 10)
    input1.draw(win)
    input2 = Entry(Point(200, 150), 10)
    input2.draw(win)[/COLOR]
    
    SNIP

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

    [COLOR="Red"]principal = float(input1.getText())
    apr = float(input2.getText())[/COLOR]

    # 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.
 
  • #3
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!
 

Related to Modifying Future Value Program with a Python GUI

1. What is a GUI in Python?

A GUI (Graphical User Interface) in Python is a way to create visual elements such as buttons, menus, and text boxes that allow users to interact with a program through a graphical interface. It is a popular way to create user-friendly programs and applications.

2. How can I start coding a GUI in Python?

To start coding a GUI in Python, you will need to first import the necessary libraries such as Tkinter, PyQt, or wxPython. Then, you can create a new window or frame, and add various widgets and functions to create the desired interface. There are also many tutorials and resources available online to help you get started.

3. Can I customize the appearance of my Python GUI?

Yes, you can customize the appearance of your Python GUI by using various styling and formatting options provided by the libraries. For example, you can change the font, color, and size of the text and buttons, add images and icons, and create custom layouts to make your GUI more visually appealing.

4. How can I add functionality to my Python GUI?

To add functionality to your Python GUI, you can use event handling methods such as bindings and callbacks. These methods allow you to link user actions, such as clicking a button, to specific functions or actions in your code. You can also use various built-in functions and methods to perform tasks such as data validation and manipulation.

5. Are there any limitations to using Python for GUI development?

While Python is a powerful language for GUI development, there are some limitations to keep in mind. For example, it may not be the best choice for creating high-performance or graphics-intensive applications. It also has a limited set of built-in GUI elements, so you may need to use third-party libraries for more advanced features. However, for most basic GUI applications, Python is a great choice.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
920
  • Programming and Computer Science
Replies
22
Views
3K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
3
Views
908
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top