I have created a currency conversion app as users need to input the currency from, to and the amount required.
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()
I have successfully retrieved the data from the API, when I click the button the result label was shown all the data below rather than the date and result of the currency’s conversion. How could I display the date and result in my result label?
The retrieved data from the API:
{ "success": true, "query": { "from": "JPY", "to": "USD", "amount": 30000 }, "info": { "timestamp": 1658636823, "rate": 0.007343 }, "date": "2022-07-24", "result": 220.29 }
Advertisement
Answer
Use result = response.json()
to get the JSON data from the response as a python dictionary. You can then use this dictionary to get the data you want.
result = response.json() result_str = "Date: " + result["date"] + " Result: " + str(result["result"]) result_label = Label(root, text=result_str)