-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathCurrency_convertor_GUI.py
91 lines (69 loc) · 2.91 KB
/
Currency_convertor_GUI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import requests
import tkinter as tk
from tkinter import ttk
def calculate_conversion():
# URL of respective API
url = "https://api.exchangerate-api.com/v4/latest/INR"
# Receive Data from API
data = requests.get(url).json()
currency_rates = data['rates']
# get From amount from GUI
try:
amount = float(from_amount.get())
answer.config(text="Successful !")
except ValueError:
answer.config(text="*Invalid input.Please enter number.")
# Get country code from GUI
fc = from_currency_code.get()
tc = to_currency_code.get()
# Logic to convert amount to INR (if county code is not INR)
if fc != 'INR':
amount = amount/currency_rates[fc]
# INR to To_country code
amount = amount * currency_rates[tc]
amount = round(amount, 2)
# Set amount to Label in GUI
to_amount.config(text=str(amount))
if __name__ == '__main__':
# url and data extraction
url = "https://api.exchangerate-api.com/v4/latest/INR"
data = requests.get(url).json()
currency_rates = data['rates']
# Building of GUI
screen = tk.Tk()
screen.title("Currency convertor")
screen.geometry("500x300")
screen.config(bg="#282828")
# Introduction Label
main_label = tk.Label(screen, text=" Welcome to Currency Convertor ")
main_label.config(font=("Lato", 15, "bold"),
anchor="center", bg='#3500D3', fg='white')
main_label.place(x=70, y=10)
# from_amount input field and placing
from_amount = tk.Entry(screen, justify=tk.CENTER)
from_amount.place(x=58, y=180)
# Converted amount label and it's placing
to_amount = tk.Label(screen, anchor="center", bg='white',
fg='black', width=16, font=("Lato", 12))
to_amount.place(x=300, y=180)
# Variable declation for dropdown menu and set default values
from_currency_code = tk.StringVar(screen)
from_currency_code.set("INR")
to_currency_code = tk.StringVar(screen)
to_currency_code.set("INR")
# dropdown menu for from_currency and it's placing
from_currency_menu = ttk.Combobox(screen, textvariable=from_currency_code, values=list(
currency_rates.keys()), font=("Lato", 12), state='readonly', width=14, justify=tk.CENTER)
from_currency_menu.place(x=61, y=110)
# dropdown menu for to_currency and it's placing
to_currency_menu = ttk.Combobox(screen, textvariable=to_currency_code, values=list(
currency_rates.keys()), font=("Lato", 12), state='readonly', width=14, justify=tk.CENTER)
to_currency_menu.place(x=303, y=110)
answer = tk.Label(screen, anchor="center", bg='#282828',
fg='red', width=50, font=("Lato", 12), text="")
answer.place(x=50, y=220)
# Convert button and placing
convert_btn = tk.Button(
screen, text="Convert", fg='white', bg="#3500D3", command=calculate_conversion)
convert_btn.place(x=230, y=260)
screen.mainloop()