1 Replies - 967 Views - Last Post: 10 August 2012 - 08:16 PM Rate Topic: -----

#1 sur4lfe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-August 12

problem with the graphics.py module

Posted 10 August 2012 - 04:06 PM

In the following code i can not get the user input for principal and apr to be graphed. I have spent several hours on this and have tried many things. I think the two lines
principal =eval(input.getText())
apr=eval(input.getText())
need to have a value in .getText()) parentheses and in the input.setText need to have something to.
this is for the Zelle book and it uses the graphics.py Lib. It is attached to the post.
# futval_graph2.py

from graphics import *

def main():
    # Introduction
    print("This program plots the growth of a 10-year investment.")

    # Get principal and interest rate
    ipwin=GraphWin("Enter interest and Principal",400,300)
    ipwin.setCoords(0.0,0.0,3.0,4.0)
    Text(Point(1,3), "Input the Principal").draw(ipwin)
    input=Entry(Point(2,3), 8)
    input.setText("")
    input.draw(ipwin)
    
    Text(Point(1,2), "Input the Interest").draw(ipwin)
    input=Entry(Point(2,2), 8)
    input.setText("")
    input.draw(ipwin)
    
    b=Text(Point(1.5,.5), "Graph it!").draw(ipwin)
    Rectangle(Point(1,0),Point(2,1)).draw(ipwin)
     
    ipwin.getMouse()

    principal =eval(input.getText())
    apr=eval(input.getText())
    

    # 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)
    
    ipwin.getMouse()
    ipwin.close()


main()



Is This A Good Question/Topic? 0
  • +

Replies To: problem with the graphics.py module

#2 sur4lfe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-August 12

Re: problem with the graphics.py module

Posted 10 August 2012 - 08:16 PM

I could not find a simple solution so I did this is anyone has a way to make it look nicer please let me know.
# futval_graph2.py

from graphics import *

def main():


    p=princ()
    a=apr()
    graph(p,a)

def princ():

    ipwin=GraphWin("Enter interest and Principal",400,300)
    ipwin.setCoords(0.0,0.0,3.0,4.0)
    Text(Point(1,2), "Input the principal").draw(ipwin)
    input=Entry(Point(2,2), 8)
    input.setText("")
    input.draw(ipwin)
    b=Text(Point(1.5,.5), "Click to input APR").draw(ipwin)
    Rectangle(Point(1,0),Point(2,1)).draw(ipwin)
    ipwin.getMouse()
    principal = eval(input.getText())
    
    ipwin.close()
    return(principal)

def apr():    

    ipwin=GraphWin("Enter interest and Principal",400,300)
    ipwin.setCoords(0.0,0.0,3.0,4.0)
    Text(Point(1,2), "Input the Interest").draw(ipwin)
    input=Entry(Point(2,2), 8)
    input.setText("")
    input.draw(ipwin)
    b=Text(Point(1.5,.5), "Graph it!").draw(ipwin)
    Rectangle(Point(1,0),Point(2,1)).draw(ipwin)
    ipwin.getMouse()
    apr = eval(input.getText())
    
    ipwin.close()

    return(apr)
    
def graph(principal,apr):
# 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)
    
    win.getMouse()
    win.close()
    
main()


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1