3

更新: 这只是创建 btComputePayment 时的一个错字。

解决了


我正在使用“Python 编程简介(Pearson 2013)”一书学习 Python。

目前我正在做一个练习,我必须在 Tkinter 中编写一个贷款计算器。

我似乎无法让以下代码工作:

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

这是完整的追溯:

/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

有任何想法吗?

谢谢

4

1 回答 1

6

您在第 33 行使用标签而不是按钮。标签不接受构造函数中的参数“命令”。当您单击按钮时,按钮使用“命令”参数来回调它们所做的事情。

于 2013-04-14T12:28:27.013 回答