Update: It was just a typo while creating btComputePayment.
SOLVED
I'm learning Python using the book "Introduction to Programming Using Python (Pearson 2013)".
Currently I'm doing an exercise in which I have to code a loan calculator in Tkinter.
I can't seem to get the following code to work:
from tkinter import *
class LoanCalculator:
def __init__(self):
window = Tk()
window.title("Loan Calculator")
# Create labels
Label(window, text = "Annual Interest Rate").grid(row = 1, column = 1, sticky = W)
Label(window, text = "Number of Years").grid(row = 2, column = 1, sticky = W)
Label(window, text = "Loan Amount").grid(row = 3, column = 1, sticky = W)
Label(window, text = "Monthly Payment").grid(row = 4, column = 1, sticky = W)
Label(window, text = "Total Payment").grid(row = 5, column = 1, sticky = W)
# Create entries
self.annualInterestRateVar = StringVar()
Entry(window, textvariable = self.annualInterestRateVar, justify = RIGHT).grid(row = 1, column = 2)
self.numberOfYearsVar = StringVar()
Entry(window, textvariable = self.numberOfYearsVar, justify = RIGHT).grid(row = 2, column = 2)
self.loanAmountVar = StringVar()
Entry(window, textvariable = self.loanAmountVar, justify = RIGHT).grid(row = 3, column = 2)
self.monthlyPaymentVar = StringVar()
lblMonthlyPayment = Label(window, textvariable = self.monthlyPaymentVar, justify = RIGHT).grid(\
row = 4, column = 2)
self.totalPaymentVar = StringVar()
lblTotalPayment = Label(window, textvariable = self.totalPaymentVar, justify = RIGHT).grid(\
row = 4, column = 2)
btComputePayment = Label(window, text = "Compute Payment", command = self.computePayment).grid(\
row = 6, column = 2, sticky = E)
window.mainloop()
def computePayment(self):
monthlyPayment = self.getMonthlyPayment(
float(self.loanAmountVar.get()),
float(self.annualInterestRateVar.get()) / 1200,
int(self.numberOfYearsVar.get()))
# Set monthly payment
self.monthlyPaymentVar.set(format(monthlyPayment, "10.2f"))
totalPayment = float(self.monthlyPaymentVar.get()) * 12 \
* int(self.numberOfYearsVar.get())
# Set total payment
self.totalPaymentVar.set(format(totalPayment, "10.2f"))
def getMonthlyPayment(self, loanAmount, monthlyInterest, numberOfYears):
monthlyPayment = loanAmount * monthlyInterest / (1 - 1 / (1 + monthlyInterest) ** (numberOfYears * 12))
return monthlyPayment
LoanCalculator()
Here's the full Traceback:
/Library/Frameworks/Python.framework/Versions/3.3/bin/python3 "/Users/narek_a/Dropbox/Python/PycharmProjects/Introduction to Programming/Chapter 9.py"
Traceback (most recent call last):
File "/Users/narek_a/Dropbox/Python/PycharmProjects/Introduction to Programming/Chapter 9.py", line 421, in <module>
LoanCalculator()
File "/Users/narek_a/Dropbox/Python/PycharmProjects/Introduction to Programming/Chapter 9.py", line 398, in __init__
btComputePayment = Label(window, text = "Compute Payment", command = self.computePayment).grid(\
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/tkinter/__init__.py", line 2596, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/tkinter/__init__.py", line 2075, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: unknown option "-command"
Process finished with exit code 1
Any ideas?
Thanks
You are using a Label instead of a Button in line 33. Labels don't accept the argument "command" in the constructor. Buttons use the "command" argument for the callback of what they do when you click in the button.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With