Jul-16-2018, 05:35 AM
(This post was last modified: Jul-16-2018, 06:20 AM by juliabrushett.)

Hello,

I am wanting to do some calculations using interest, total amount, and years to pay of an item, but I am having trouble getting and using the user input from a tkinter entry. Can someone help guide me through this process? My code so far is below:

Ignore the StringVar "a" on line 21, that was something else I was trying that I forgot to remove from the code before posting

Okay, adjusted again. Now I am just having trouble calling my function, I think. Here is my error message when trying to run code:

Traceback (most recent call last):

File "C:\Users\Julia\Desktop\Python\Assignment16\Assignment16\Assignment16.py", line 39, in <module>

monthpayment = calculatePayment()

File "C:\Users\Julia\Desktop\Python\Assignment16\Assignment16\Assignment16.py", line 11, in calculatePayment

amt = float(amount.get())

ValueError: could not convert string to float:

Press any key to continue . . .

Here is my adjusted code:

Okay, I figured it out! For anyone who may want to see, here is my final code:

I am wanting to do some calculations using interest, total amount, and years to pay of an item, but I am having trouble getting and using the user input from a tkinter entry. Can someone help guide me through this process? My code so far is below:

import math from tkinter import * def calculatePayment(amount, intRate, years) : amount = entAmount.get() intRate = entIntRate.get() years = entYears.get() interest = intRate / 100 interest = interest / 12 payment = (amount * interest) / (1 - (math.pow(1 / (1 + interest), years * 12))) #lblMonthlyPayment = Label(main, text = payment) #lblTotalPayment = Label(main, text = payment * 12 * years) return payment main = Tk() main.title("How Much?") main.geometry('300x300') lblAmount = Label(main, text = 'Amount of Purchase:') lblAmount.grid(row = 0, column = 0, padx = 0, pady = 10) a = StringVar() entAmount = Entry(main, width = 20) entAmount.grid(row = 0, column = 1) lblIntRate = Label(main, text = 'Interest Rate (like 7.5):') lblIntRate.grid(row = 1, column = 0, padx = 0, pady = 10) entIntRate = Entry(main, width = 20) entIntRate.grid(row = 1, column = 1) lblYears = Label(main, text = 'Years to Pay:') lblYears.grid(row = 2, column = 0, padx = 0, pady = 10) entYears = Entry(main, width = 20) entYears.grid(row = 2, column = 1) lblMonthly = Label(main, text = 'Monthly Payment:') lblMonthly.grid(row = 3, column = 0, padx = 0, pady = 10) lblMonthly.bind(calculatePayment) lblTotal = Label(main, text = 'Total Purchase Cost:') lblTotal.grid(row = 4, column = 0, padx = 0, pady = 10) btn = Button(BOTTOM, text = 'Calculate', command = calculatePayment) btn.pack() main.mainloop()

Ignore the StringVar "a" on line 21, that was something else I was trying that I forgot to remove from the code before posting

Okay, adjusted again. Now I am just having trouble calling my function, I think. Here is my error message when trying to run code:

Traceback (most recent call last):

File "C:\Users\Julia\Desktop\Python\Assignment16\Assignment16\Assignment16.py", line 39, in <module>

monthpayment = calculatePayment()

File "C:\Users\Julia\Desktop\Python\Assignment16\Assignment16\Assignment16.py", line 11, in calculatePayment

amt = float(amount.get())

ValueError: could not convert string to float:

Press any key to continue . . .

Here is my adjusted code:

import math from tkinter import * def calculatePayment() : amt = float(amount.get()) itrt = float(intRate.get()) yrs = float(years.get()) interest = itrt / 100 interest = interest / 12 payment = (amt * interest) / (1 - (math.pow(1 / (1 + interest), yrs * 12))) return payment main = Tk() main.title("How Much?") main.geometry('300x300') amount = StringVar() intRate = StringVar() years = StringVar() lblAmount = Label(main, text = 'Amount of Purchase:').grid(row = 0, column = 0, padx = 0, pady = 10) entAmount = Entry(main, textvariable = amount).grid(row = 0, column = 1) lblIntRate = Label(main, text = 'Interest Rate (like 7.5):').grid(row = 1, column = 0, padx = 0, pady = 10) entIntRate = Entry(main, textvariable = intRate).grid(row = 1, column = 1) lblYears = Label(main, text = 'Years to Pay:').grid(row = 2, column = 0, padx = 0, pady = 10) entYears = Entry(main, textvariable = years).grid(row = 2, column = 1) btn = Button(main, text = 'Calculate', command = calculatePayment).grid(row = 5, column = 1) monthpayment = calculatePayment() lblMonthly = Label(main, text = 'Monthly Payment: $ %.2f' % monthpayment).grid(row = 3, column = 0, padx = 0, pady = 10) totpayment = monthpayment * 12 * float(years) lblTotal = Label(main, text = 'Total Purchase Cost:' % totpayment).grid(row = 4, column = 0, padx = 0, pady = 10) main.mainloop()

Okay, I figured it out! For anyone who may want to see, here is my final code:

import math from tkinter import * def calculatePayment() : amt = float(amount.get()) itrt = float(intRate.get()) yrs = float(years.get()) interest = itrt / 100 interest = interest / 12 payment = (amt * interest) / (1 - (math.pow(1 / (1 + interest), yrs * 12))) lblMonthly = Label(main, text = '$ %.2f' % payment).grid(row = 3, column = 1, padx = 0, pady = 10) totpayment = payment * 12 * yrs lblTotal = Label(main, text = '$ %.2f' % totpayment).grid(row = 4, column = 1, padx = 0, pady = 10) return main = Tk() main.title("How Much?") main.geometry('300x300') amount = StringVar() intRate = StringVar() years = StringVar() lblAmount = Label(main, text = 'Amount of Purchase:').grid(row = 0, column = 0, padx = 0, pady = 10) entAmount = Entry(main, textvariable = amount).grid(row = 0, column = 1) lblIntRate = Label(main, text = 'Interest Rate (like 7.5):').grid(row = 1, column = 0, padx = 0, pady = 10) entIntRate = Entry(main, textvariable = intRate).grid(row = 1, column = 1) lblYears = Label(main, text = 'Years to Pay:').grid(row = 2, column = 0, padx = 0, pady = 10) entYears = Entry(main, textvariable = years).grid(row = 2, column = 1) btn = Button(main, text = 'Calculate', command = calculatePayment).grid(row = 5, column = 1) lblMonthly = Label(main, text = 'Monthly Payment:').grid(row = 3, column = 0, padx = 0, pady = 10) lblTotal = Label(main, text = 'Total Purchase Cost:').grid(row = 4, column = 0, padx = 0, pady = 10) main.mainloop()