Skip to content

Commit 0af10ef

Browse files
authored
Create currency_converter.py
1 parent 1e725e1 commit 0af10ef

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

Diff for: currency_converter.py

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#import modules
2+
from tkinter import *
3+
import requests
4+
import json
5+
from bs4 import BeautifulSoup
6+
7+
#scraping currency code and exchange rate from "https://www.iban.com/exchange-rates"
8+
currency_codes_url = "https://www.iban.com/exchange-rates";
9+
currency_codes_url_res = BeautifulSoup(requests.get(currency_codes_url).content , 'html.parser')
10+
currencyNameToSymbolHtml = currency_codes_url_res.tbody.find_all('tr')
11+
12+
#declaring dicts
13+
currencyNameToSymbol = {}
14+
currencySymbolWithEuroExchangeRate = {}
15+
16+
#getting country name,symbol,and rate in euro and mapping them to the dicts
17+
for currencyInf in currencyNameToSymbolHtml:
18+
currencyInfList = list(currencyInf.find_all('td'))
19+
currencyName = str(currencyInfList[1].string);
20+
currencySymbol = currencyInfList[0].img.next_sibling.strip();
21+
currencyInEuro = float(currencyInfList[2].string)
22+
23+
currencyNameToSymbol[currencyName] = currencySymbol
24+
currencySymbolWithEuroExchangeRate[currencyName] = currencyInEuro
25+
26+
27+
28+
29+
#configuring tkinter
30+
top = Tk()
31+
top.geometry("450x250")
32+
top.configure(bg='#222831')
33+
34+
#making the dropdown menus for choosing currencies
35+
BASE_OPTIONS = list(currencyNameToSymbol.keys())
36+
TO_OPTIONS = list(currencyNameToSymbol.keys())
37+
38+
base = StringVar(top)
39+
base.set(BASE_OPTIONS[0]) # default value
40+
41+
to = StringVar(top)
42+
to.set(TO_OPTIONS[0]) # default value
43+
44+
baseOptionsMenu = OptionMenu(top, base, *BASE_OPTIONS)
45+
toOptionsMenu = OptionMenu(top, to, *TO_OPTIONS)
46+
resultLabel = Label(top,bg='#222831' , foreground='#fff')
47+
48+
#making a number only entry
49+
def callback(P):
50+
if str.isdigit(P) or P == "":
51+
return True
52+
else:
53+
return False
54+
55+
vcmd = (top.register(callback))
56+
amountInput = Entry(top ,validate='all', validatecommand=(vcmd, '%P'))
57+
58+
59+
baseOptionsMenu.configure(width=15 , height=2,bg='#1f6f8b')
60+
toOptionsMenu.configure(width=15 , height=2,bg='#1f6f8b')
61+
62+
#placing the widgets in the screen using grid
63+
baseOptionsMenu.grid(row=1 , column=3 , pady=(250//2 - 70 , 5) , padx=(10 ,10))
64+
toOptionsMenu.grid(row=1 , column=5 , pady=(250//2 - 70 , 5) , padx=(10 ,10))
65+
amountInput.grid(row=1 , column=4 , pady=(250//2 - 70 , 5))
66+
resultLabel.grid(row=2 , column=4)
67+
68+
#function to calculate result
69+
def returnResult():
70+
71+
baseInEuro = currencySymbolWithEuroExchangeRate[base.get()]
72+
toInEuro = currencySymbolWithEuroExchangeRate[to.get()]
73+
74+
result = (float(toInEuro) / float(baseInEuro))* int(amountInput.get())
75+
resultLabel['text'] = f'{currencyNameToSymbol[to.get()]} {result}'
76+
77+
button = Button(top, text="Convert", command=returnResult , width=7 , height=1,bg="#ff4d4d")
78+
button.grid(row=3 , column=4,padx=(0 ,10) , pady=(10))
79+
80+
#mainloop
81+
mainloop()

0 commit comments

Comments
 (0)