Python Forum
[Tkinter] Getting Input from Tkinter Entry
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Getting Input from Tkinter Entry
#1
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()
Reply
#2
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
#3
(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
#4
reblark -- why are you answering posts that are more than a year and a half old?
Reply
#5
(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
#6
Grace and wisdom, huh?
Reply
#7
Not a problem, I was just curious.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Making entry global in tkinter with multiprocessing luckyingermany 2 2,284 Jan-21-2022, 03:46 PM
Last Post: deanhystad
  Tkinter Exit Code based on Entry Widget Nu2Python 6 2,874 Oct-21-2021, 03:01 PM
Last Post: Nu2Python
  [Tkinter] Update variable using tkinter entry methon drSlump 6 5,093 Oct-15-2021, 08:01 AM
Last Post: drSlump
  Tkinter | entry output. Sap2ch 1 1,949 Sep-25-2021, 12:38 AM
Last Post: Yoriz
  .get() from generated Entry widgets in tkinter snakes 4 4,155 May-03-2021, 11:26 PM
Last Post: snakes
  Entry Validation in tkinter shahulvk 4 16,026 Oct-28-2020, 10:12 PM
Last Post: joe_momma
  Convert combobox user input in to date with tkinter Ame 8 6,656 Jul-01-2020, 09:40 PM
Last Post: Yoriz
  Converting Entry field value to integer in tkinter scratchmyhead 2 4,883 May-11-2020, 03:41 PM
Last Post: scratchmyhead
  [Tkinter] Tkinter adding entry values scratchmyhead 1 2,164 May-04-2020, 05:21 AM
Last Post: Yoriz
  [Tkinter] Entry box not showing 2 decimal places Chuck_Norwich 3 5,617 Apr-24-2020, 05:28 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020