|
| 1 | +import requests |
| 2 | +import tkinter as tk |
| 3 | +from tkinter import ttk |
| 4 | + |
| 5 | +def calculate_conversion(): |
| 6 | + #URL of respective API |
| 7 | + url = "https://api.exchangerate-api.com/v4/latest/INR" |
| 8 | + |
| 9 | + #Receive Data from API |
| 10 | + data = requests.get(url).json() |
| 11 | + currency_rates = data['rates'] |
| 12 | + |
| 13 | + #get From amount from GUI |
| 14 | + amount = float(from_amount.get()) |
| 15 | + |
| 16 | + #Get country code from GUI |
| 17 | + fc = from_currency_code.get() |
| 18 | + tc = to_currency_code.get() |
| 19 | + |
| 20 | + #Logic to convert amount to INR (if county code is not INR) |
| 21 | + if fc != 'INR': |
| 22 | + amount = amount/currency_rates[fc] |
| 23 | + |
| 24 | + #INR to To_country code |
| 25 | + amount = amount * currency_rates[tc] |
| 26 | + amount = round(amount,2) |
| 27 | + |
| 28 | + #Set amount to Label in GUI |
| 29 | + to_amount.config(text = str(amount)) |
| 30 | + |
| 31 | + |
| 32 | +if __name__ == '__main__': |
| 33 | + |
| 34 | + #url and data extraction |
| 35 | + url = "https://api.exchangerate-api.com/v4/latest/INR" |
| 36 | + data = requests.get(url).json() |
| 37 | + currency_rates = data['rates'] |
| 38 | + |
| 39 | + #Building of GUI |
| 40 | + screen = tk.Tk() |
| 41 | + screen.title("Currency convertor") |
| 42 | + screen.geometry("500x300") |
| 43 | + screen.config(bg = "#282828") |
| 44 | + |
| 45 | + #Introduction Label |
| 46 | + main_label = tk.Label(screen,text=" Welcome to Currency Convertor ") |
| 47 | + main_label.config(font = ("Lato",15,"bold"), anchor = "center" , bg = '#3500D3' , fg = 'white') |
| 48 | + main_label.place(x = 70,y = 10) |
| 49 | + |
| 50 | + #from_amount input field and placing |
| 51 | + from_amount = tk.Entry(screen , justify = tk.CENTER) |
| 52 | + from_amount.place(x=58,y = 180) |
| 53 | + |
| 54 | + #Converted amount label and it's placing |
| 55 | + to_amount = tk.Label(screen, anchor = "center" , bg = 'white' , fg = 'black', width = 16, font = ("Lato",12)) |
| 56 | + to_amount.place(x=300,y = 180) |
| 57 | + |
| 58 | + #Variable declation for dropdown menu and set default values |
| 59 | + from_currency_code = tk.StringVar(screen) |
| 60 | + from_currency_code.set("INR") |
| 61 | + |
| 62 | + to_currency_code = tk.StringVar(screen) |
| 63 | + to_currency_code.set("INR") |
| 64 | + |
| 65 | + #dropdown menu for from_currency and it's placing |
| 66 | + 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) |
| 67 | + from_currency_menu.place(x = 61,y = 110) |
| 68 | + |
| 69 | + #dropdown menu for to_currency and it's placing |
| 70 | + 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) |
| 71 | + to_currency_menu.place(x = 303,y = 110) |
| 72 | + |
| 73 | + #Convert button and placing |
| 74 | + convert_btn = tk.Button(screen, text = "Convert" , fg = 'white' , bg = "#3500D3", command = calculate_conversion) |
| 75 | + convert_btn.place(x = 230,y = 240) |
| 76 | + |
| 77 | + screen.mainloop() |
| 78 | + |
| 79 | + |
0 commit comments