-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.py
376 lines (273 loc) · 15.8 KB
/
converter.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import sys
import tkinter as tk
from tkinter import *
import urllib.request
import webbrowser
from functools import partial
from tkinter import Tk, StringVar , ttk
###################################################################
root = Tk()
root.title('ALL IN ONE CONVERTER')
root.geometry("450x400+100+200")
labelfont = ('ariel', 56, 'bold')
l=Label(root,text='ALL IN ONE CONVERTER',font = ("Arial", 20, "italic"), justify = CENTER)
l.place(x=80,y=20)
widget = Button(None, text="QUIT", bg="white", fg="red",font = ("Arial", 14, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "green", activeforeground="blue", command=root.destroy).place(x=350,y=350)
#############################################################################################################################################
def CurrencyConverter():
ids = {"US Dollar" : 'USD', "Euros" : 'EUR', "Indian Rupees" : 'INR', "Qatar Doha" : 'QAR', "Zimbwabe Harare" : 'ZWD', "Arab Emirates Dirham" : 'AED', "Pound Sterling" : 'GBP', "Japanese Yen" : 'JPY', "Yuan Renminbi" : 'CNY'}
def convert(amt, frm, to):
html =urllib.request.urlopen("http://www.exchangerate-api.com/%s/%s/%f?k=a28d653d2d4fd2727003e437" % (frm , to, amt))
return html.read().decode('utf-8')
def callback():
try:
amt = float(in_field.get())
except ValueError:
out_amt.set('Invalid input')
return None
if in_unit.get() == 'Select Unit' or out_unit.get() == 'Select Unit':
out_amt.set('Input or output unit not chosen')
return None
else:
frm = ids[in_unit.get()]
to = ids[out_unit.get()]
out_amt.set(convert(amt, frm, to))
root = Toplevel()
root.title("Currency Converter")
# initiate frame
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.pack(fill=BOTH, expand=1)
titleLabel = Label (mainframe, text = "Currency Converter", font = ("Arial", 12, "bold"), justify = CENTER).grid(column=1,row=1)
in_amt = StringVar()
in_amt.set('0')
out_amt = StringVar()
in_unit = StringVar()
out_unit = StringVar()
in_unit.set('Select Unit')
out_unit.set('Select Unit')
# Add input field
in_field = ttk.Entry(mainframe, width=20, textvariable=in_amt)
in_field.grid(row=1, column=2, sticky=(W, E))
# Add drop-down for input unit
in_select = OptionMenu(mainframe, in_unit, "US Dollar", "Euros", "Indian Rupees", "Qatar Doha", "Zimbwabe Harare", "Arab Emirates Dirham", "Pound Sterling", "Japanese Yen", "Yuan Renminbi").grid(column=3, row=1, sticky=W)
# Add output field and drop-down
ttk.Entry(mainframe, textvariable=out_amt, state="readonly").grid(column=2, row=3, sticky=(W, E))
in_select = OptionMenu(mainframe, out_unit, "US Dollar", "Euros", "Indian Rupees", "Qatar Doha", "Zimbwabe Harare", "Arab Emirates Dirham", "Pound Sterling", "Japanese Yen", "Yuan Renminbi").grid(column=3, row=3, sticky=W)
calc_button = ttk.Button(mainframe, text="Calculate",command=callback).grid(column=2, row=2, sticky=E)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
in_field.focus()
#################################################################################################
##############################################################################################################333
def WeightConverter():
# factors to multiply to a value to convert from the following units to meters(m)
factors = {'kg' : 1000, 'hg' : 100, 'dg' : 10, 'g' : 1,'deg' : 0.1, 'cg' : 0.01, 'mg' : 0.001}
ids = {"Kilogram" : 'kg', "Hectagram" : 'hg', "Decagram" : 'dg', "Decigram" : 'deg', "Kilogram" : 'kg', "gram" : 'g', "centigram" : 'cg', "milligram" : 'mg'}
# function to convert from a given unit to another
def convert(amt, frm, to):
if frm != 'g':
amt = amt * factors[frm]
return amt / factors[to]
else:
return amt / factors[to]
def callback():
try:
amt = float(in_field.get())
except ValueError:
out_amt.set('Invalid input')
return None
if in_unit.get() == 'Select Unit' or out_unit.get() == 'Select Unit':
out_amt.set('Input or output unit not chosen')
return None
else:
frm = ids[in_unit.get()]
to = ids[out_unit.get()]
out_amt.set(convert(amt, frm, to))
# initiate window
root = Toplevel()
root.title("Weight Converter")
# initiate frame
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.pack(fill=BOTH, expand=1)
titleLabel = Label (mainframe, text = "Weight Converter", font = ("Arial", 12, "bold"), justify = CENTER).grid(column=1,row=1)
in_amt = StringVar()
in_amt.set('0')
out_amt = StringVar()
in_unit = StringVar()
out_unit = StringVar()
in_unit.set('Select Unit')
out_unit.set('Select Unit')
# Add input field
in_field = ttk.Entry(mainframe, width=20, textvariable=in_amt)
in_field.grid(row=1, column=2, sticky=(W, E))
# Add drop-down for input unit
in_select = OptionMenu(mainframe, in_unit, "Kilogram","Hectagram","Decagram", "gram", "Decigram","Centigram", "Milligram") .grid(column=3, row=1, sticky=W)
# Add output field and drop-down
ttk.Entry(mainframe, textvariable=out_amt, state="readonly").grid(column=2, row=3, sticky=(W, E))
in_select = OptionMenu(mainframe, out_unit, "Kilogram","Hectagram","Decagram", "gram", "Decigram","Centigram", "Milligram").grid(column=3, row=3, sticky=W)
calc_button = ttk.Button(mainframe, text="Calculate", command=callback).grid(column=2, row=2, sticky=E)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
in_field.focus()
###########################################################################################################
def AreaConverter():
wind = Toplevel()
wind.minsize(width=400, height=150)
wind.maxsize(width=400, height=150)
meterFactor = {'square meter':1,'square km':1000000,'square rood':1011.7141056,'square cm':0.0001,'square foot':0.09290304 ,
'square inch':0.00064516, 'square mile':2589988.110336, 'milimeter':0.000001,'square rod':25.29285264,
'square yard':0.83612736, 'square township':93239571.9721, 'square acre':4046.8564224 ,'square are': 100,
'square barn':1e-28, 'square hectare':10000, 'square homestead':647497.027584 }
def convert(x, fromUnit, toUnit):
if fromVar.get() in meterFactor.keys() and toVar.get() in meterFactor.keys():
resultxt.delete(0, END)
result = (float(str(x))*meterFactor[fromUnit])/(meterFactor[toUnit])
resultxt.insert(0, str(result))
titleLabel = Label (wind, text = "Area Converter", font = ("Arial", 12, "bold"), justify = CENTER).grid(column=1,row=1)
e = Entry(wind)
e.grid(row = 1, column = 2)
values = list(meterFactor.keys())
fromVar = StringVar(wind)
toVar = StringVar(wind)
fromVar.set("From Unit")
toVar.set("To Unit")
fromOption = OptionMenu(wind, fromVar, *values, command= lambda y: convert(e.get(), fromVar.get() ,toVar.get()))
fromOption.grid(row=1, column = 3)
toLabel = Label(wind, text="To : ", font="Arial").grid(row=2, column = 2)
toOption = OptionMenu(wind, toVar, *values, command= lambda x: convert(e.get(), fromVar.get() ,toVar.get()))
toOption.grid(row=3, column = 3)
resultxt = Entry(wind)
resultxt.grid(row=3, column=2)
#############################################################################################################################################################
def LengthConverter():
# factors to multiply to a value to convert from the following units to meters(m)
factors = {'nmi' : 1852, 'mi' : 1609.34, 'yd' : 0.9144, 'ft' : 0.3048, 'inch' : 0.0254, 'km' : 1000, 'm' : 1, 'cm' : 0.01, 'mm' : 0.001}
ids = {"Nautical Miles" : 'nmi', "Miles" : 'mi', "Yards" : 'yd', "Feet" : 'ft', "Inches" : 'inch', "Kilometers" : 'km', "meters" : 'm', "centimeters" : 'cm', "millileters" : 'mm'}
# function to convert from a given unit to another
def convert(amt, frm, to):
if frm != 'm':
amt = amt * factors[frm]
return amt / factors[to]
else:
return amt / factors[to]
def callback():
try:
amt = float(in_field.get())
except ValueError:
out_amt.set('Invalid input')
return None
if in_unit.get() == 'Select Unit' or out_unit.get() == 'Select Unit':
out_amt.set('Input or output unit not chosen')
return None
else:
frm = ids[in_unit.get()]
to = ids[out_unit.get()]
out_amt.set(convert(amt, frm, to))
# initiate window
root = Toplevel()
root.title("Length Converter")
# initiate frame
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.pack(fill=BOTH, expand=1)
titleLabel = Label (mainframe, text = "Length Converter", font = ("Arial", 12, "bold"), justify = CENTER).grid(column=1,row=1)
in_amt = StringVar()
in_amt.set('0')
out_amt = StringVar()
in_unit = StringVar()
out_unit = StringVar()
in_unit.set('Select Unit')
out_unit.set('Select Unit')
# Add input field
in_field = ttk.Entry(mainframe, width=20, textvariable=in_amt)
in_field.grid(row=1, column=2, sticky=(W, E))
# Add drop-down for input unit
in_select = OptionMenu(mainframe, in_unit, "Nautical Miles", "Miles", "Yards", "Feet", "Inches", "Kilometers", "meters", "centimeters", "millileters").grid(column=3, row=1, sticky=W)
# Add output field and drop-down
ttk.Entry(mainframe, textvariable=out_amt, state="readonly").grid(column=2, row=3, sticky=(W, E))
in_select = OptionMenu(mainframe, out_unit, "Nautical Miles", "Miles", "Yards", "Feet", "Inches", "Kilometers", "meters", "centimeters", "millileters").grid(column=3, row=3, sticky=W)
calc_button = ttk.Button(mainframe, text="Calculate", command=callback).grid(column=2, row=2, sticky=E)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
in_field.focus()
###################################################################################################################################################################
def TemperatureConverter():
def convert():
celTemp = celTempVar.get()
fahTemp = fahTempVar.get()
if celTempVar.get() != 0.0:
celToFah = (celTemp * 9/5 + 32)
fahTempVar.set(celToFah)
elif fahTempVar.get() != 0.0:
fahToCel = ((fahTemp - 32) * (5/9))
celTempVar.set(fahToCel)
def reset():
top = Toplevel(padx=50, pady=50)
top.grid()
message = Label(top, text = "Reset Complete")
button = Button(top, text="OK", command=top.destroy)
message.grid(row = 0, padx = 5, pady = 5)
button.grid(row = 1, ipadx = 10, ipady = 10, padx = 5, pady = 5)
fahTempVar.set(int(0))
celTempVar.set(int(0))
top = Toplevel()
top.title("Temperature Converter")
###MAIN###
celTempVar = IntVar()
celTempVar.set(int(0))
fahTempVar = IntVar()
fahTempVar.set(int(0))
titleLabel = Label (top, text = "Temperature Converter", font = ("Arial", 12, "bold"), justify = CENTER).grid(column=1,row=1)
celLabel = Label (top, text = "Celcius: ", font = ("Arial", 16), fg = "red")
celLabel.grid(row = 2, column = 1, pady = 10, sticky = NW)
fahLabel = Label (top, text = "Fahrenheit: ", font = ("Arial", 16), fg = "blue")
fahLabel.grid(row = 3, column = 1, pady = 10, sticky = NW)
celEntry = Entry (top, width = 10, bd = 5, textvariable = celTempVar)
celEntry.grid(row = 2, column = 1, pady = 10, sticky = NW, padx = 125 )
fahEntry = Entry (top, width = 10, bd = 5, textvariable = fahTempVar)
fahEntry.grid(row = 3, column = 1, pady = 10, sticky = NW, padx = 125 )
convertButton =Button (top, text = "Convert", font = ("Arial", 8, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "green", activeforeground="blue", command = convert)
convertButton.grid(row = 4, column = 1, ipady = 8, ipadx = 12, pady = 5, sticky = NW, padx = 55)
resetButton = Button (top, text = "Reset", font = ("Arial", 8, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "green", activeforeground="blue", command = reset)
resetButton.grid(row = 4, column = 2,ipady = 8, ipadx = 12, pady = 5, sticky = NW)
###################################################################################################################################################################################
def sensex(event):
webbrowser.open_new(r"#")
def nifty(event):
webbrowser.open_new(r"#")
def gold(event):
webbrowser.open_new(r"#")
def silver(event):
webbrowser.open_new(r"#")
####################################################################################
#Hovering
def color_config(widget, color, event):
widget.configure(foreground=color)
text =Label(root, text="SENSEX",font = ("Arial", 14, "bold"))
text.bind("<Enter>", partial(color_config, text, "red"))
text.bind("<Leave>", partial(color_config, text, "blue"))
text.pack()
text.bind("<Button-1>",sensex)
text.place(x=350,y=120)
text =Label(root, text="NIFTY",font = ("Arial", 14, "bold"))
text.bind("<Enter>", partial(color_config, text, "red"))
text.bind("<Leave>", partial(color_config, text, "blue"))
text.pack()
text.bind("<Button-1>",nifty)
text.place(x=350,y=150)
text =Label(root, text="GOLD",font = ("Arial", 14, "bold"))
text.bind("<Enter>", partial(color_config, text, "red"))
text.bind("<Leave>", partial(color_config, text, "blue"))
text.pack()
text.bind("<Button-1>",gold)
text.place(x=350,y=180)
text =Label(root, text="SILVER",font = ("Arial", 14, "bold"))
text.bind("<Enter>", partial(color_config, text, "red"))
text.bind("<Leave>", partial(color_config, text, "blue"))
text.pack()
text.bind("<Button-1>",silver)
text.place(x=350,y=210)
####################################################################################################
#TEMPERATURE CONVERTER
widget = Button(root, text="Temperature converter", bg="white" , fg="red",font = ("Arial", 14, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "green", activeforeground="blue", command=TemperatureConverter).place(x=50,y=120)
widget = Button(root, text="Length Converter", bg="white" , fg="red",font = ("Arial", 14, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "green", activeforeground="blue", command=LengthConverter).place(x=50,y=180)
widget = Button(root, text="Area Converter", bg="white" , fg="red",font = ("Arial", 14, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "green", activeforeground="blue", command=AreaConverter).place(x=50,y=240)
widget = Button(root, text="Currency converter", bg="white" , fg="red",font = ("Arial", 14, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "green", activeforeground="blue", command=CurrencyConverter).place(x=50,y=60)
widget = Button(root, text="Weight Converter", bg="white" , fg="red",font = ("Arial", 14, "bold"), relief = RAISED, bd=5, justify = CENTER, highlightbackground = "red", overrelief = GROOVE, activebackground = "green", activeforeground="blue", command=WeightConverter).place(x=50,y=300)
root.mainloop()