转换应用程序以显示日期和结果



我创建了一个货币转换应用程序,因为用户需要输入货币from、to和所需金额。

import requests
from tkinter import *
root = Tk()
root.title('Currency Converter')
root.geometry("500x460")
def currency_convertion():
url = "https://api.apilayer.com/exchangerates_data/convert?to=" + to_currency_entry.get() + "&from=" + from_currency_entry.get() + "&amount=" + amount_entry.get()
payload = {}
headers= {
"apikey": ""
}
response = requests.request("GET", url, headers=headers, data = payload)
status_code = response.status_code
result = response.text
print(result)
result_label = Label(root, text=result)
result_label.grid(row=0, column= 1, columnspan=2)

from_currency_label = Label(root, text='From Currency')
from_currency_label.grid(row=1, column=0, pady=10)
to_currency_label = Label(root, text='To Currency')
to_currency_label.grid(row=2, column=0, pady=10)

amount_label = Label(root, text='Amount')
amount_label.grid(row=3, column=0, pady=10)
from_currency_entry = Entry(root,)
from_currency_entry.grid(row=1, column=1, stick=W+E+N+S, pady=10)
to_currency_entry = Entry(root,)
to_currency_entry.grid(row=2, column=1, stick=W+E+N+S,pady=10)

amount_entry = Entry(root,)
amount_entry.grid(row=3, column=1, stick=W+E+N+S, pady=10)
button = Button(root, text="Convert", command=currency_convertion)
button.grid(row=4, column=1, stick=W+E+N+S)
root.mainloop()

我已经成功地从API检索到数据,当我单击按钮时,结果标签显示的是下面的所有数据,而不是货币转换的日期和结果。如何在结果标签中显示日期和结果?

从API检索的数据:

{
"success": true,
"query": {
"from": "JPY",
"to": "USD",
"amount": 30000
},
"info": {
"timestamp": 1658636823,
"rate": 0.007343
},
"date": "2022-07-24",
"result": 220.29
}

使用result = response.json()从响应中获取JSON数据作为python字典。然后,您可以使用此字典来获取所需的数据。

result = response.json()
result_str = "Date: " + result["date"] + " Result: " + str(result["result"])
result_label = Label(root, text=result_str)

最新更新