Bottom Page

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()
Quote
#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')
Quote
#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')
Quote
#4
reblark -- why are you answering posts that are more than a year and a half old?
Quote
#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.
Quote
#6
Grace and wisdom, huh?
Quote
#7
Not a problem, I was just curious.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Converting Entry field value to integer in tkinter scratchmyhead 2 110 May-11-2020, 03:41 PM
Last Post: scratchmyhead
  [Tkinter] Tkinter adding entry values scratchmyhead 1 148 May-04-2020, 05:21 AM
Last Post: Yoriz
  [Tkinter] Entry box not showing 2 decimal places Chuck_Norwich 3 151 Apr-24-2020, 05:28 PM
Last Post: deanhystad
  Always lowercase entry in tkinter ReturnName 8 301 Apr-18-2020, 09:41 AM
Last Post: PeroPuri
  Can't Get Entry Var in Tkinter OCaraDoBaixo 1 600 Feb-18-2020, 06:08 PM
Last Post: Larz60+
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 1,032 Jan-23-2020, 09:00 PM
Last Post: HBH
  Newbie question with Tkinter Entry mariolopes 2 346 Oct-12-2019, 11:02 PM
Last Post: Larz60+
  [Tkinter] how to input a random entry with each button press? nadavrock 1 1,332 Jun-17-2019, 05:28 AM
Last Post: Yoriz
  [Tkinter] how to get the entry information using Entry.get() ? SamyPyth 2 992 Mar-18-2019, 05:36 PM
Last Post: woooee
  [Tkinter] Tkinter Entry widget and KeyPad intrgration chiragarya1992 0 2,206 Aug-11-2018, 01:09 AM
Last Post: chiragarya1992

Forum Jump:


Users browsing this thread: 1 Guest(s)