gpt4 book ai didi

python - tkinter.TclError : unknown option 错误

转载 作者:行者123 更新时间:2023-11-28 22:56:12 28 4
gpt4 key购买 nike

更新:这只是创建 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

有什么想法吗?

谢谢

最佳答案

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

关于python - tkinter.TclError : unknown option 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15996935/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com