gpt4 book ai didi

python - TypeError : 'CurrencyConverter' object is not callable

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

我正在尝试创建货币转换器,并且遇到了这个问题。不太确定我需要做些什么才能克服它。我将所有代码都放在这里,因为我根本不知道在哪里寻找问题。我是新手,因此非常感谢您提供的任何帮助。

from tkinter import *
from currency_converter import CurrencyConverter

global c
c = CurrencyConverter()
#test converter
# result = c.convert(100, 'USD', 'HKD')
# print(result)
#create window
root = Tk()
root.title("EZ Currency Converter")
root.geometry("350x200")

#create currency option list
currencies = ["USD","EUR","JPY", "GBP","AUD", "CAD", "CHF","HKD","SGD", "MXN",
"SEK", "PHP", "RUB", "HUF","LTL","MTL", "PLN", "ROL", "SIT", "SKK",
]

def conversion(clicked, second_clicked, currency_amount):
result= c(currency_amount, clicked, second_clicked)
final_conversion = Label(root, text= result)
final_conversion.grid(row=4, column=1, columnspan=2)


#Set what kind of variable to expect
clicked = StringVar()
clicked.set("USD")
second_clicked= StringVar()
second_clicked.set("USD")

#welcome new users
welcome = Label(root, text="Welcome to your new favorite Currency Converter")

#create the dropdown options/ input box / button
original =OptionMenu(root, clicked, *currencies)
converted =OptionMenu(root, second_clicked, *currencies)
currency_amount= Entry(root, width=30)
convert = Button(root, text="Convert", padx=10, pady=10, command=lambda:conversion(clicked.get(),second_clicked.get(), currency_amount.get()))


#place widgets
welcome.grid(row=0, column=1, columnspan=2)
original.grid(row=1, column= 1, columnspan=1)
converted.grid(row=1, column=2, columnspan=1)
currency_amount.grid(row=2, column=1, columnspan=2)
convert.grid(row=3, column=1, columnspan=2)

root.mainloop()

最佳答案

首先定义c:

c = CurrencyConverter()
然后在 conversion中,对象 c称为:
result = c(currency_amount, clicked, second_clicked)
这与以下内容相同:
result = CurrencyConverter()(currency_amount, clicked, second_clicked)
因此,当我们尝试在对象 c上调用函数时,这是一个错误。
它可能看起来应该像这样:
result = c.convert(currency_amount, clicked, second_clicked)

关于python - TypeError : 'CurrencyConverter' object is not callable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63672232/

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