I have created a currency conversion app as users need to input the currency from, to and the amount required.
JavaScript
x
47
47
1
import requests
2
from tkinter import *
3
4
root = Tk()
5
root.title('Currency Converter')
6
root.geometry("500x460")
7
8
def currency_convertion():
9
url = "https://api.apilayer.com/exchangerates_data/convert?to=" + to_currency_entry.get() + "&from=" + from_currency_entry.get() + "&amount=" + amount_entry.get()
10
11
payload = {}
12
headers= {
13
"apikey": ""
14
}
15
16
response = requests.request("GET", url, headers=headers, data = payload)
17
18
status_code = response.status_code
19
result = response.text
20
print(result)
21
result_label = Label(root, text=result)
22
result_label.grid(row=0, column= 1, columnspan=2)
23
24
25
from_currency_label = Label(root, text='From Currency')
26
from_currency_label.grid(row=1, column=0, pady=10)
27
28
to_currency_label = Label(root, text='To Currency')
29
to_currency_label.grid(row=2, column=0, pady=10)
30
31
amount_label = Label(root, text='Amount')
32
amount_label.grid(row=3, column=0, pady=10)
33
34
from_currency_entry = Entry(root,)
35
from_currency_entry.grid(row=1, column=1, stick=W+E+N+S, pady=10)
36
37
to_currency_entry = Entry(root,)
38
to_currency_entry.grid(row=2, column=1, stick=W+E+N+S,pady=10)
39
40
amount_entry = Entry(root,)
41
amount_entry.grid(row=3, column=1, stick=W+E+N+S, pady=10)
42
43
button = Button(root, text="Convert", command=currency_convertion)
44
button.grid(row=4, column=1, stick=W+E+N+S)
45
46
root.mainloop()
47
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:
JavaScript
1
15
15
1
{
2
"success": true,
3
"query": {
4
"from": "JPY",
5
"to": "USD",
6
"amount": 30000
7
},
8
"info": {
9
"timestamp": 1658636823,
10
"rate": 0.007343
11
},
12
"date": "2022-07-24",
13
"result": 220.29
14
}
15
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.
JavaScript
1
4
1
result = response.json()
2
result_str = "Date: " + result["date"] + " Result: " + str(result["result"])
3
result_label = Label(root, text=result_str)
4