##### [Tkinter] Getting Input from Tkinter Entry
 [Tkinter] Getting Input from Tkinter Entry juliabrushett Programmer named Tim Posts: 14 Threads: 9 Joined: Jun 2018 Reputation: 0 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: ```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 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()``` Reply Larz60+ aetate et sapientia Posts: 10,018 Threads: 372 Joined: Sep 2016 Reputation: 423 Jul-16-2018, 09:06 AM (This post was last modified: Jul-16-2018, 09:06 AM by Larz60+.) 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')``` Reply reblark Unladen Swallow Posts: 3 Threads: 0 Joined: Feb 2020 Reputation: 0 Feb-12-2020, 11:33 PM (This post was last modified: Feb-12-2020, 11:34 PM by reblark.) (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') Reply Larz60+ aetate et sapientia Posts: 10,018 Threads: 372 Joined: Sep 2016 Reputation: 423 Feb-12-2020, 11:39 PM reblark -- why are you answering posts that are more than a year and a half old? Reply reblark Unladen Swallow Posts: 3 Threads: 0 Joined: Feb 2020 Reputation: 0 Feb-13-2020, 12:12 AM (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. Reply reblark Unladen Swallow Posts: 3 Threads: 0 Joined: Feb 2020 Reputation: 0 May-29-2020, 11:56 PM Grace and wisdom, huh? Reply Larz60+ aetate et sapientia Posts: 10,018 Threads: 372 Joined: Sep 2016 Reputation: 423 May-30-2020, 03:29 PM Not a problem, I was just curious. Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Entry Validation in tkinter shahulvk 4 318 Oct-28-2020, 10:12 PM Last Post: joe_momma Convert combobox user input in to date with tkinter Ame 8 1,421 Jul-01-2020, 09:40 PM Last Post: Yoriz Converting Entry field value to integer in tkinter scratchmyhead 2 789 May-11-2020, 03:41 PM Last Post: scratchmyhead [Tkinter] Tkinter adding entry values scratchmyhead 1 465 May-04-2020, 05:21 AM Last Post: Yoriz [Tkinter] Entry box not showing 2 decimal places Chuck_Norwich 3 847 Apr-24-2020, 05:28 PM Last Post: deanhystad Always lowercase entry in tkinter ReturnName 8 1,072 Apr-18-2020, 09:41 AM Last Post: PeroPuri Can't Get Entry Var in Tkinter OCaraDoBaixo 1 899 Feb-18-2020, 06:08 PM Last Post: Larz60+ Transfer Toplevel window entry to root window entry with TKinter HBH 0 1,488 Jan-23-2020, 09:00 PM Last Post: HBH Newbie question with Tkinter Entry mariolopes 2 631 Oct-12-2019, 11:02 PM Last Post: Larz60+ [Tkinter] how to input a random entry with each button press? nadavrock 1 2,087 Jun-17-2019, 05:28 AM Last Post: Yoriz

Forum Jump:

### User Panel Messages ##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020