5
5
BASE_URL = "http://api.exchangeratesapi.io/v1/latest"
6
6
API_KEY = "Your API Key"
7
7
8
+
8
9
class CurrencyConverterApp :
9
10
def __init__ (self , root ):
10
11
self .root = root
11
12
self .root .title ("Currency Converter" )
12
13
self .root .configure (bg = "black" )
13
14
14
-
15
- self . amount_label = tk . Label ( root , text = "Amount:" , fg = "white" , bg = "black" )
15
+ self . amount_label = tk . Label (
16
+ root , text = "Amount:" , fg = "white" , bg = "black" )
16
17
self .amount_label .pack ()
17
18
18
-
19
-
20
19
self .amount_entry = tk .Entry (root )
21
20
self .amount_entry .pack ()
22
21
23
-
24
-
25
- self .from_label = tk .Label (root , text = "From Currency:" , fg = "white" , bg = "black" )
22
+ self .from_label = tk .Label (
23
+ root , text = "From Currency:" , fg = "white" , bg = "black" )
26
24
self .from_label .pack ()
27
25
28
26
self .from_currency_entry = tk .Entry (root )
29
27
self .from_currency_entry .pack ()
30
28
31
-
32
-
33
-
34
- self .to_label = tk .Label (root , text = "To Currency:" , fg = "white" , bg = "black" )
29
+ self .to_label = tk .Label (
30
+ root , text = "To Currency:" , fg = "white" , bg = "black" )
35
31
self .to_label .pack ()
36
32
37
33
self .to_currency_entry = tk .Entry (root )
38
34
self .to_currency_entry .pack ()
39
35
40
-
41
-
42
- self .convert_button = tk .Button (root , text = "Convert" , command = self .convert_currency )
36
+ self .convert_button = tk .Button (
37
+ root , text = "Convert" , command = self .convert_currency )
43
38
self .convert_button .pack ()
44
39
45
-
46
-
47
40
def get_rates (self ):
48
41
payload = {"access_key" : API_KEY }
49
42
@@ -61,8 +54,6 @@ def get_currency(self, currency, rates):
61
54
else :
62
55
raise ValueError (f"{ currency } is not a valid currency" )
63
56
64
-
65
-
66
57
def convert_currency (self ):
67
58
amount = float (self .amount_entry .get ())
68
59
@@ -75,10 +66,11 @@ def convert_currency(self):
75
66
from_rate = self .get_currency (from_currency , rates )
76
67
to_rate = self .get_currency (to_currency , rates )
77
68
78
-
79
69
conversion = round ((to_rate / from_rate ) * amount , 2 )
80
70
81
- messagebox .showinfo ("Conversion Result" , f"{ amount :.2f} ({ from_currency } ) is { conversion :.2f} ({ to_currency } )" )
71
+ messagebox .showinfo (
72
+ "Conversion Result" , f"{ amount :.2f} ({ from_currency } ) is { conversion :.2f} ({ to_currency } )" )
73
+
82
74
83
75
def main ():
84
76
app = tk .Tk ()
@@ -87,8 +79,8 @@ def main():
87
79
88
80
currency_converter = CurrencyConverterApp (app )
89
81
90
-
91
82
app .mainloop ()
92
83
84
+
93
85
if __name__ == "__main__" :
94
86
main ()
0 commit comments