from graphics import * def createForm(win): p_t = Text(Point(50, 30), "Principal") p_t.draw(win) p_t.setSize(18) p_t.setStyle("bold") p_t.setTextColor("blue") p_e = Entry(Point(250, 30), 40) p_e.draw(win) r_t = Text(Point(50, 60), "Rate") r_t.draw(win) r_t.setSize(18) r_t.setStyle("bold") r_t.setTextColor("blue") r_e = Entry(Point(250, 60), 40) r_e.draw(win) y_t = Text(Point(50, 90), "# of Ys") y_t.draw(win) y_t.setSize(18) y_t.setStyle("bold") y_t.setTextColor("blue") y_e = Entry(Point(250, 90), 40) y_e.draw(win) r = Rectangle(Point(40, 140), Point(390, 120)) r.draw(win) r.setFill("blue") rt = Text(r.getCenter(), "Compute") rt.draw(win) rt.setTextColor("white") rt.setStyle("bold") rt.setSize(14) bar_list = [] bottom_bar_labels = [] top_bar_labels = [] while True: try: c = win.getMouse() while not (c.getX() > r.getP1().getX() and c.getX() < r.getP2().getX() and c.getY() < r.getP1().getY() and c.getY() > r.getP2().getY()): c = win.getMouse() except: break try: principal = float(p_e.getText()) rate = float(r_e.getText()) years = int(y_e.getText()) p_e.setText("") r_e.setText("") y_e.setText("") for i in range(len(bar_list)): bar_list[i].undraw() bottom_bar_labels[i].undraw() top_bar_labels[i].undraw() bar_list.clear() bottom_bar_labels.clear() top_bar_labels.clear() #To keep things simple (and fit all bars in the window), we assume that the principal cannot exceed 5000 and the number of years cannot exceed 10. if 0.0 <= rate <= 0.3 and 0 <= years <= 10 and 0 <= principal <= 5000: print("Principal = ", principal, "Rate = ", rate, "Num of Years = ", years) initial_scale = 30 bar_width = 35 x1 = 30 y1 = 600 x2 = x1 + bar_width y2 = y1 - initial_scale bar = Rectangle(Point(x1, y1), Point(x2, y2)) bar.setFill("red") bar.draw(win) bar_list.append(bar) b_label = Text(Point(x1+10, y1+10), 0) b_label.draw(win) bottom_bar_labels.append(b_label) t_label = Text(Point(bar.getCenter().getX(), y2-10), round(principal, 1)) t_label.setFace("courier") t_label.setSize(8) t_label.draw(win) top_bar_labels.append(t_label) for i in range(1, years+1): compounded_pr = calcInterest(principal, rate, i) x1 = x2 x2 = x1 + bar_width y2 = y1 - (compounded_pr * initial_scale/principal) bar = Rectangle(Point(x1, y1), Point(x2, y2)) bar.setFill("red") bar.draw(win) bar_list.append(bar) b_label = Text(Point(x1+10, y1+10), i) b_label.draw(win) bottom_bar_labels.append(b_label) t_label = Text(Point(bar.getCenter().getX(), y2-10), round(compounded_pr, 1)) t_label.setFace("courier") t_label.setSize(8) t_label.draw(win) top_bar_labels.append(t_label) else: print("The principal should not exceed 2000, the rate value should be between 0 and 1, and the number of years should be less than or equal 10.") except: print("You should enter a float/int value for the principal, a float value between 0 and 1 for the rate, and an int value for the number of years.") def calcInterest(p, r, y): return p * ((1 + r) ** y) win = GraphWin("Interest Calculation and Plotting", 800, 800) createForm(win)