Amazing-Python-Scripts

Форк
0
91 строка · 2.9 Кб
1
import requests
2
import tkinter as tk
3
from tkinter import ttk
4

5

6
def calculate_conversion():
7
    # URL of respective API
8
    url = "https://api.exchangerate-api.com/v4/latest/INR"
9

10
    # Receive Data from API
11
    data = requests.get(url).json()
12
    currency_rates = data['rates']
13

14
    # get From amount from GUI
15
    try:
16
        amount = float(from_amount.get())
17
        answer.config(text="Successful !")
18
    except ValueError:
19
        answer.config(text="*Invalid input.Please enter number.")
20

21
    # Get country code from GUI
22
    fc = from_currency_code.get()
23
    tc = to_currency_code.get()
24

25
    # Logic to convert amount to INR (if county code is not INR)
26
    if fc != 'INR':
27
        amount = amount/currency_rates[fc]
28

29
    # INR to To_country code
30
    amount = amount * currency_rates[tc]
31
    amount = round(amount, 2)
32

33
    # Set amount to Label in GUI
34
    to_amount.config(text=str(amount))
35

36

37
if __name__ == '__main__':
38

39
    # url and data extraction
40
    url = "https://api.exchangerate-api.com/v4/latest/INR"
41
    data = requests.get(url).json()
42
    currency_rates = data['rates']
43

44
    # Building of GUI
45
    screen = tk.Tk()
46
    screen.title("Currency convertor")
47
    screen.geometry("500x300")
48
    screen.config(bg="#282828")
49

50
    # Introduction Label
51
    main_label = tk.Label(screen, text=" Welcome to Currency Convertor ")
52
    main_label.config(font=("Lato", 15, "bold"),
53
                      anchor="center", bg='#3500D3', fg='white')
54
    main_label.place(x=70, y=10)
55

56
    # from_amount input field and placing
57
    from_amount = tk.Entry(screen, justify=tk.CENTER)
58
    from_amount.place(x=58, y=180)
59

60
    # Converted amount label and it's placing
61
    to_amount = tk.Label(screen, anchor="center", bg='white',
62
                         fg='black', width=16, font=("Lato", 12))
63
    to_amount.place(x=300, y=180)
64

65
    # Variable declation for dropdown menu and set default values
66
    from_currency_code = tk.StringVar(screen)
67
    from_currency_code.set("INR")
68

69
    to_currency_code = tk.StringVar(screen)
70
    to_currency_code.set("INR")
71

72
    # dropdown menu for from_currency and it's placing
73
    from_currency_menu = ttk.Combobox(screen, textvariable=from_currency_code, values=list(
74
        currency_rates.keys()), font=("Lato", 12), state='readonly', width=14, justify=tk.CENTER)
75
    from_currency_menu.place(x=61, y=110)
76

77
    # dropdown menu for to_currency and it's placing
78
    to_currency_menu = ttk.Combobox(screen, textvariable=to_currency_code, values=list(
79
        currency_rates.keys()), font=("Lato", 12), state='readonly', width=14, justify=tk.CENTER)
80
    to_currency_menu.place(x=303, y=110)
81

82
    answer = tk.Label(screen, anchor="center", bg='#282828',
83
                      fg='red', width=50, font=("Lato", 12), text="")
84
    answer.place(x=50, y=220)
85

86
    # Convert button and placing
87
    convert_btn = tk.Button(
88
        screen, text="Convert", fg='white', bg="#3500D3", command=calculate_conversion)
89
    convert_btn.place(x=230, y=260)
90

91
    screen.mainloop()
92

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.