-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
162 lines (93 loc) · 3.99 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import tkinter,requests
from tkinter import messagebox
import json
from PIL import Image,ImageTk
SYMBOLS = ['EUR','JPY','CAD','MXN','CNY','GBP','AUD','BRL','NZD','RUB','ZAR','INR','HKD','CZK','RON','SGD','CHF','SGD','KRW','ILS','USD']
SYMBOLS_STRING = ','.join(SYMBOLS)
def convert():
try:
dollars = float(starting_currency_entry.get())
except ValueError:
messagebox.showerror(title='Oops',message='Please enter a NUMBER!')
starting_currency_entry.delete(0,tkinter.END)
else:
base = from_variable.get()
to = to_variable.get()
if dollars < 0:
messagebox.showerror(title='Oops',message='Please enter a POSITIVE NUMBER!')
starting_currency_entry.delete(0,tkinter.END)
else:
url = f'https://api.exchangeratesapi.io/latest'
d = {'base': base,'symbols': SYMBOLS_STRING}
response = requests.get(url,params=d)
exchange_rates = json.loads(response.text)
if to == base:
exchange_rate = 1
else:
exchange_rate =euros_per_dollar = exchange_rates['rates'][to]
result = dollars * exchange_rate
ending_currency_result['text'] = f"{result:.2f}"
window = tkinter.Tk()
window.title("Currency Converter")
window.config(padx=20,pady=20)
font = ('Courier',40,"bold")
canvas = tkinter.Canvas(width=320,height=320,highlightthickness=0)
image = Image.open('currency.png')
photo = ImageTk.PhotoImage(image)
canvas.create_image(160,160,image=photo)
canvas.grid(row=0,column=0,columnspan=2)
starting_currency_label = tkinter.Label(text="USD:",font=font)
starting_currency_label.grid(row=1,column=0,sticky=tkinter.E)
starting_currency_entry = tkinter.Entry(font=font)
starting_currency_entry.insert(0,"0.00")
starting_currency_entry.focus()
starting_currency_entry.grid(row=1,column=1,sticky=tkinter.W,padx=5)
ending_currency_label = tkinter.Label(text="EUROS:",font=font)
ending_currency_label.grid(row=2,column=0,sticky=tkinter.E)
ending_currency_result = tkinter.Label(text='0.00',font=font)
ending_currency_result.grid(row=2,column=1,sticky=tkinter.W,padx=5)
frame = tkinter.Frame()
frame.grid(row=3,column=0)
def radio_used():
value = radio_state.get()
print('used')
if value == 1:
ending_currency_label['text'] = 'EUROS:'
starting_currency_label['text'] = 'USD:'
else:
ending_currency_label['text'] = 'USD:'
starting_currency_label['text'] = 'EUROS:'
ending_currency_result['text']= '0.00'
def from_changed(*args):
starting_currency_label['text'] = f"{from_variable.get()}:"
ending_currency_result['text'] = '0.00'
def to_changed(*args):
ending_currency_label['text'] = f"{to_variable.get()}:"
ending_currency_result['text'] = '0.00'
from_variable = tkinter.StringVar()
from_variable.set('USD')
from_variable.trace('w',from_changed)
option_menu_1_label = tkinter.Label(text='FROM:',font=font)
option_menu_1_label.grid(row=3,column=0)
option_menu_1 = tkinter.OptionMenu(window,from_variable,*SYMBOLS)
option_menu_1.config(font=font)
option_menu_1.grid(row=3,column=1,sticky=tkinter.W)
to_variable = tkinter.StringVar()
to_variable.trace('w',to_changed)
to_variable.set('EUR')
option_menu_2_label = tkinter.Label(text='TO:',font=font)
option_menu_2_label.grid(row=4,column=0)
option_menu_2 = tkinter.OptionMenu(window,to_variable,*SYMBOLS)
option_menu_2.config(font=font)
option_menu_2.grid(row=4,column=1,sticky=tkinter.W)
'''
radio_state = tkinter.IntVar()
radio_state.set(1)
radiobutton1 = tkinter.Radiobutton(frame,text='USD to EURO',value=1,variable=radio_state,command=radio_used,font=font)
radiobutton1.grid(row=0,column=0)
radiobutton2 = tkinter.Radiobutton(frame,text='EURO to USD',value=2,variable=radio_state,command=radio_used,font=font)
radiobutton2.grid(row=1,column=0)
'''
convert_button = tkinter.Button(text='CONVERT',font=font,command=convert)
convert_button.grid(row=5,column=0,columnspan=2)
window.mainloop()