Python Forum
[Tkinter] Getting Input from Tkinter Entry - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Getting Input from Tkinter Entry (/thread-11565.html)



Getting Input from Tkinter Entry - juliabrushett - Jul-16-2018

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:
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()



RE: Getting Input from Tkinter Entry - Larz60+ - Jul-16-2018

You are trying (adjusted code, line 5 for example) to get amount before it exists.
move the definitions for the entry textvariables (lines 20, 21, 22) before you try to access.
You must also set the values to something valid before access, example:
amount = StringVar()
amount.set('0')



RE: Getting Input from Tkinter Entry - reblark - Feb-12-2020

(Jul-16-2018, 09:06 AM)Larz60+ Wrote: You are trying (adjusted code, line 5 for example) to get amount before it exists.
move the definitions for the entry textvariables (lines 20, 21, 22) before you try to access.
You must also set the values to something valid before access, example:
amount = StringVar()
amount.set('0')
I used this code for another project and it worked without setting the values, like "amount.set('0')


RE: Getting Input from Tkinter Entry - Larz60+ - Feb-12-2020

reblark -- why are you answering posts that are more than a year and a half old?


RE: Getting Input from Tkinter Entry - reblark - Feb-13-2020

(Feb-12-2020, 11:39 PM)Larz60+ Wrote: reblark -- why are you answering posts that are more than a year and a half old?

I didn't notice the date because your post appeared after I read the original post. I am a newbie at this and I am error prone. Experience people in Internet forums seem to have little patience with newbies. Perhaps you might forgive me. I signed up here because my experiences with Stack Overflow were simply horrible.


RE: Getting Input from Tkinter Entry - reblark - May-29-2020

Grace and wisdom, huh?


RE: Getting Input from Tkinter Entry - Larz60+ - May-30-2020

Not a problem, I was just curious.