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