|
| 1 | +from tkinter import * |
| 2 | +import urllib.request |
| 3 | +import webbrowser |
| 4 | +from functools import partial |
| 5 | +from tkinter import Tk, StringVar , ttk |
| 6 | + |
| 7 | +################################################################### |
| 8 | + |
| 9 | +root = Tk() |
| 10 | +root.title('ALL IN ONE CONVERTER') |
| 11 | +root.geometry("450x400+100+200") |
| 12 | +labelfont = ('ariel', 56, 'bold') |
| 13 | +l=Label(root,text='ALL IN ONE CONVERTER',font = ("Arial", 20, "italic"), justify = CENTER) |
| 14 | +l.place(x=80,y=20) |
| 15 | + |
| 16 | +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) |
| 17 | + |
| 18 | +############################################################################################################################################# |
| 19 | + |
| 20 | +def CurrencyConverter(): |
| 21 | + |
| 22 | + 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'} |
| 23 | + |
| 24 | + def convert(amt, frm, to): |
| 25 | + html =urllib.request.urlopen("http://www.exchangerate-api.com/%s/%s/%f?k=a28d653d2d4fd2727003e437" % (frm , to, amt)) |
| 26 | + return html.read().decode('utf-8') |
| 27 | + |
| 28 | + def callback(): |
| 29 | + try: |
| 30 | + amt = float(in_field.get()) |
| 31 | + |
| 32 | + except ValueError: |
| 33 | + out_amt.set('Invalid input') |
| 34 | + return None |
| 35 | + if in_unit.get() == 'Select Unit' or out_unit.get() == 'Select Unit': |
| 36 | + out_amt.set('Input or output unit not chosen') |
| 37 | + return None |
| 38 | + else: |
| 39 | + frm = ids[in_unit.get()] |
| 40 | + to = ids[out_unit.get()] |
| 41 | + out_amt.set(convert(amt, frm, to)) |
| 42 | + |
| 43 | + root = Toplevel() |
| 44 | + root.title("Currency Converter") |
| 45 | + |
| 46 | + # initiate frame |
| 47 | + mainframe = ttk.Frame(root, padding="3 3 12 12") |
| 48 | + mainframe.pack(fill=BOTH, expand=1) |
| 49 | + titleLabel = Label (mainframe, text = "Currency Converter", font = ("Arial", 12, "bold"), justify = CENTER).grid(column=1,row=1) |
| 50 | + in_amt = StringVar() |
| 51 | + in_amt.set('0') |
| 52 | + out_amt = StringVar() |
| 53 | + |
| 54 | + in_unit = StringVar() |
| 55 | + out_unit = StringVar() |
| 56 | + in_unit.set('Select Unit') |
| 57 | + out_unit.set('Select Unit') |
| 58 | + |
| 59 | + # Add input field |
| 60 | + in_field = ttk.Entry(mainframe, width=20, textvariable=in_amt) |
| 61 | + in_field.grid(row=1, column=2, sticky=(W, E)) |
| 62 | + |
| 63 | + # Add drop-down for input unit |
| 64 | + 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) |
| 65 | + |
| 66 | + # Add output field and drop-down |
| 67 | + ttk.Entry(mainframe, textvariable=out_amt, state="readonly").grid(column=2, row=3, sticky=(W, E)) |
| 68 | + 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) |
| 69 | + |
| 70 | + calc_button = ttk.Button(mainframe, text="Calculate",command=callback).grid(column=2, row=2, sticky=E) |
| 71 | + |
| 72 | + for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5) |
| 73 | + |
| 74 | + in_field.focus() |
| 75 | + |
| 76 | +################################################################################################# |
| 77 | + |
| 78 | +def WeightConverter(): |
| 79 | + # factors to multiply to a value to convert from the following units to meters(m) |
| 80 | + factors = {'kg' : 1000, 'hg' : 100, 'dg' : 10, 'g' : 1,'deg' : 0.1, 'cg' : 0.01, 'mg' : 0.001} |
| 81 | + ids = {"Kilogram" : 'kg', "Hectagram" : 'hg', "Decagram" : 'dg', "Decigram" : 'deg', "Kilogram" : 'kg', "gram" : 'g', "centigram" : 'cg', "milligram" : 'mg'} |
| 82 | + # function to convert from a given unit to another |
| 83 | + def convert(amt, frm, to): |
| 84 | + if frm != 'g': |
| 85 | + amt = amt * factors[frm] |
| 86 | + return amt / factors[to] |
| 87 | + else: |
| 88 | + return amt / factors[to] |
| 89 | + |
| 90 | + def callback(): |
| 91 | + try: |
| 92 | + amt = float(in_field.get()) |
| 93 | + except ValueError: |
| 94 | + out_amt.set('Invalid input') |
| 95 | + return None |
| 96 | + if in_unit.get() == 'Select Unit' or out_unit.get() == 'Select Unit': |
| 97 | + out_amt.set('Input or output unit not chosen') |
| 98 | + return None |
| 99 | + else: |
| 100 | + frm = ids[in_unit.get()] |
| 101 | + to = ids[out_unit.get()] |
| 102 | + out_amt.set(convert(amt, frm, to)) |
| 103 | + |
| 104 | + # initiate window |
| 105 | + root = Toplevel() |
| 106 | + root.title("Weight Converter") |
| 107 | + |
| 108 | + # initiate frame |
| 109 | + mainframe = ttk.Frame(root, padding="3 3 12 12") |
| 110 | + mainframe.pack(fill=BOTH, expand=1) |
| 111 | + titleLabel = Label (mainframe, text = "Weight Converter", font = ("Arial", 12, "bold"), justify = CENTER).grid(column=1,row=1) |
| 112 | + |
| 113 | + in_amt = StringVar() |
| 114 | + in_amt.set('0') |
| 115 | + out_amt = StringVar() |
| 116 | + |
| 117 | + in_unit = StringVar() |
| 118 | + out_unit = StringVar() |
| 119 | + in_unit.set('Select Unit') |
| 120 | + out_unit.set('Select Unit') |
| 121 | + |
| 122 | + # Add input field |
| 123 | + in_field = ttk.Entry(mainframe, width=20, textvariable=in_amt) |
| 124 | + in_field.grid(row=1, column=2, sticky=(W, E)) |
| 125 | + |
| 126 | + # Add drop-down for input unit |
| 127 | + in_select = OptionMenu(mainframe, in_unit, "Kilogram","Hectagram","Decagram", "gram", "Decigram","Centigram", "Milligram") .grid(column=3, row=1, sticky=W) |
| 128 | + |
| 129 | + # Add output field and drop-down |
| 130 | + ttk.Entry(mainframe, textvariable=out_amt, state="readonly").grid(column=2, row=3, sticky=(W, E)) |
| 131 | + in_select = OptionMenu(mainframe, out_unit, "Kilogram","Hectagram","Decagram", "gram", "Decigram","Centigram", "Milligram").grid(column=3, row=3, sticky=W) |
| 132 | + |
| 133 | + calc_button = ttk.Button(mainframe, text="Calculate", command=callback).grid(column=2, row=2, sticky=E) |
| 134 | + |
| 135 | + for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5) |
| 136 | + |
| 137 | + in_field.focus() |
| 138 | + |
| 139 | +########################################################################################################### |
| 140 | + |
| 141 | +def AreaConverter(): |
| 142 | + wind = Toplevel() |
| 143 | + wind.minsize(width=400, height=150) |
| 144 | + wind.maxsize(width=400, height=150) |
| 145 | + |
| 146 | + meterFactor = {'square meter':1,'square km':1000000,'square rood':1011.7141056,'square cm':0.0001,'square foot':0.09290304 , |
| 147 | + 'square inch':0.00064516, 'square mile':2589988.110336, 'milimeter':0.000001,'square rod':25.29285264, |
| 148 | + 'square yard':0.83612736, 'square township':93239571.9721, 'square acre':4046.8564224 ,'square are': 100, |
| 149 | + 'square barn':1e-28, 'square hectare':10000, 'square homestead':647497.027584 } |
| 150 | + |
| 151 | + def convert(x, fromUnit, toUnit): |
| 152 | + if fromVar.get() in meterFactor.keys() and toVar.get() in meterFactor.keys(): |
| 153 | + resultxt.delete(0, END) |
| 154 | + result = (float(str(x))*meterFactor[fromUnit])/(meterFactor[toUnit]) |
| 155 | + resultxt.insert(0, str(result)) |
| 156 | + |
| 157 | + titleLabel = Label (wind, text = "Area Converter", font = ("Arial", 12, "bold"), justify = CENTER).grid(column=1,row=1) |
| 158 | + |
| 159 | + e = Entry(wind) |
| 160 | + e.grid(row = 1, column = 2) |
| 161 | + values = list(meterFactor.keys()) |
| 162 | + |
| 163 | + fromVar = StringVar(wind) |
| 164 | + toVar = StringVar(wind) |
| 165 | + fromVar.set("From Unit") |
| 166 | + toVar.set("To Unit") |
| 167 | + |
| 168 | + |
| 169 | + fromOption = OptionMenu(wind, fromVar, *values, command= lambda y: convert(e.get(), fromVar.get() ,toVar.get())) |
| 170 | + fromOption.grid(row=1, column = 3) |
| 171 | + |
| 172 | + toLabel = Label(wind, text="To : ", font="Arial").grid(row=2, column = 2) |
| 173 | + toOption = OptionMenu(wind, toVar, *values, command= lambda x: convert(e.get(), fromVar.get() ,toVar.get())) |
| 174 | + toOption.grid(row=3, column = 3) |
| 175 | + |
| 176 | + resultxt = Entry(wind) |
| 177 | + resultxt.grid(row=3, column=2) |
| 178 | + |
| 179 | +############################################################################################################################################################# |
| 180 | + |
| 181 | +def LengthConverter(): |
| 182 | + # factors to multiply to a value to convert from the following units to meters(m) |
| 183 | + 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} |
| 184 | + ids = {"Nautical Miles" : 'nmi', "Miles" : 'mi', "Yards" : 'yd', "Feet" : 'ft', "Inches" : 'inch', "Kilometers" : 'km', "meters" : 'm', "centimeters" : 'cm', "millileters" : 'mm'} |
| 185 | + |
| 186 | + # function to convert from a given unit to another |
| 187 | + def convert(amt, frm, to): |
| 188 | + if frm != 'm': |
| 189 | + amt = amt * factors[frm] |
| 190 | + return amt / factors[to] |
| 191 | + else: |
| 192 | + return amt / factors[to] |
| 193 | + |
| 194 | + def callback(): |
| 195 | + try: |
| 196 | + amt = float(in_field.get()) |
| 197 | + except ValueError: |
| 198 | + out_amt.set('Invalid input') |
| 199 | + return None |
| 200 | + if in_unit.get() == 'Select Unit' or out_unit.get() == 'Select Unit': |
| 201 | + out_amt.set('Input or output unit not chosen') |
| 202 | + return None |
| 203 | + else: |
| 204 | + frm = ids[in_unit.get()] |
| 205 | + to = ids[out_unit.get()] |
| 206 | + out_amt.set(convert(amt, frm, to)) |
| 207 | + |
| 208 | + # initiate window |
| 209 | + root = Toplevel() |
| 210 | + root.title("Length Converter") |
| 211 | + |
| 212 | + # initiate frame |
| 213 | + mainframe = ttk.Frame(root, padding="3 3 12 12") |
| 214 | + mainframe.pack(fill=BOTH, expand=1) |
| 215 | + titleLabel = Label (mainframe, text = "Length Converter", font = ("Arial", 12, "bold"), justify = CENTER).grid(column=1,row=1) |
| 216 | + |
| 217 | + in_amt = StringVar() |
| 218 | + in_amt.set('0') |
| 219 | + out_amt = StringVar() |
| 220 | + |
| 221 | + in_unit = StringVar() |
| 222 | + out_unit = StringVar() |
| 223 | + in_unit.set('Select Unit') |
| 224 | + out_unit.set('Select Unit') |
| 225 | + |
| 226 | + # Add input field |
| 227 | + in_field = ttk.Entry(mainframe, width=20, textvariable=in_amt) |
| 228 | + in_field.grid(row=1, column=2, sticky=(W, E)) |
| 229 | + |
| 230 | + # Add drop-down for input unit |
| 231 | + in_select = OptionMenu(mainframe, in_unit, "Nautical Miles", "Miles", "Yards", "Feet", "Inches", "Kilometers", "meters", "centimeters", "millileters").grid(column=3, row=1, sticky=W) |
| 232 | + |
| 233 | + # Add output field and drop-down |
| 234 | + ttk.Entry(mainframe, textvariable=out_amt, state="readonly").grid(column=2, row=3, sticky=(W, E)) |
| 235 | + in_select = OptionMenu(mainframe, out_unit, "Nautical Miles", "Miles", "Yards", "Feet", "Inches", "Kilometers", "meters", "centimeters", "millileters").grid(column=3, row=3, sticky=W) |
| 236 | + |
| 237 | + calc_button = ttk.Button(mainframe, text="Calculate", command=callback).grid(column=2, row=2, sticky=E) |
| 238 | + |
| 239 | + for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5) |
| 240 | + |
| 241 | + in_field.focus() |
| 242 | + |
| 243 | +################################################################################################################################################################### |
| 244 | + |
| 245 | +def TemperatureConverter(): |
| 246 | + def convert(): |
| 247 | + celTemp = celTempVar.get() |
| 248 | + fahTemp = fahTempVar.get() |
| 249 | + |
| 250 | + if celTempVar.get() != 0.0: |
| 251 | + celToFah = (celTemp * 9/5 + 32) |
| 252 | + fahTempVar.set(celToFah) |
| 253 | + |
| 254 | + elif fahTempVar.get() != 0.0: |
| 255 | + fahToCel = ((fahTemp - 32) * (5/9)) |
| 256 | + celTempVar.set(fahToCel) |
| 257 | + |
| 258 | + def reset(): |
| 259 | + top = Toplevel(padx=50, pady=50) |
| 260 | + top.grid() |
| 261 | + message = Label(top, text = "Reset Complete") |
| 262 | + button = Button(top, text="OK", command=top.destroy) |
| 263 | + |
| 264 | + message.grid(row = 0, padx = 5, pady = 5) |
| 265 | + button.grid(row = 1, ipadx = 10, ipady = 10, padx = 5, pady = 5) |
| 266 | + |
| 267 | + fahTempVar.set(int(0)) |
| 268 | + celTempVar.set(int(0)) |
| 269 | + top = Toplevel() |
| 270 | + top.title("Temperature Converter") |
| 271 | + ###MAIN### |
| 272 | + celTempVar = IntVar() |
| 273 | + celTempVar.set(int(0)) |
| 274 | + fahTempVar = IntVar() |
| 275 | + fahTempVar.set(int(0)) |
| 276 | + titleLabel = Label (top, text = "Temperature Converter", font = ("Arial", 12, "bold"), justify = CENTER).grid(column=1,row=1) |
| 277 | + |
| 278 | + celLabel = Label (top, text = "Celcius: ", font = ("Arial", 16), fg = "red") |
| 279 | + celLabel.grid(row = 2, column = 1, pady = 10, sticky = NW) |
| 280 | + |
| 281 | + fahLabel = Label (top, text = "Fahrenheit: ", font = ("Arial", 16), fg = "blue") |
| 282 | + fahLabel.grid(row = 3, column = 1, pady = 10, sticky = NW) |
| 283 | + |
| 284 | + celEntry = Entry (top, width = 10, bd = 5, textvariable = celTempVar) |
| 285 | + celEntry.grid(row = 2, column = 1, pady = 10, sticky = NW, padx = 125 ) |
| 286 | + |
| 287 | + |
| 288 | + fahEntry = Entry (top, width = 10, bd = 5, textvariable = fahTempVar) |
| 289 | + fahEntry.grid(row = 3, column = 1, pady = 10, sticky = NW, padx = 125 ) |
| 290 | + |
| 291 | + 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) |
| 292 | + convertButton.grid(row = 4, column = 1, ipady = 8, ipadx = 12, pady = 5, sticky = NW, padx = 55) |
| 293 | + |
| 294 | + 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) |
| 295 | + resetButton.grid(row = 4, column = 2,ipady = 8, ipadx = 12, pady = 5, sticky = NW) |
| 296 | + |
| 297 | + |
| 298 | +################################################################################################################################################################################### |
| 299 | + |
| 300 | +def sensex(event): |
| 301 | + webbrowser.open_new(r"https://www.google.com/search?q=sensex%20today%20live%20chart") |
| 302 | +def nifty(event): |
| 303 | + webbrowser.open_new(r"https://www.google.com/search?q=nifty%20today%20live%20chart") |
| 304 | +def gold(event): |
| 305 | + webbrowser.open_new(r"https://www.google.com/search?q=gold%20today%20live%20chart") |
| 306 | +def silver(event): |
| 307 | + webbrowser.open_new(r"https://www.moneycontrol.com/commodity/silver-price.html") |
| 308 | + |
| 309 | +#################################################################################### |
| 310 | + |
| 311 | +#Hovering |
| 312 | +def color_config(widget, color, event): |
| 313 | + widget.configure(foreground=color) |
| 314 | + |
| 315 | +text =Label(root, text="SENSEX",font = ("Arial", 14, "bold")) |
| 316 | + |
| 317 | +text.bind("<Enter>", partial(color_config, text, "red")) |
| 318 | +text.bind("<Leave>", partial(color_config, text, "blue")) |
| 319 | +text.pack() |
| 320 | +text.bind("<Button-1>",sensex) |
| 321 | +text.place(x=350,y=120) |
| 322 | +text =Label(root, text="NIFTY",font = ("Arial", 14, "bold")) |
| 323 | + |
| 324 | +text.bind("<Enter>", partial(color_config, text, "red")) |
| 325 | +text.bind("<Leave>", partial(color_config, text, "blue")) |
| 326 | +text.pack() |
| 327 | +text.bind("<Button-1>",nifty) |
| 328 | +text.place(x=350,y=150) |
| 329 | + |
| 330 | +text =Label(root, text="GOLD",font = ("Arial", 14, "bold")) |
| 331 | + |
| 332 | +text.bind("<Enter>", partial(color_config, text, "red")) |
| 333 | +text.bind("<Leave>", partial(color_config, text, "blue")) |
| 334 | +text.pack() |
| 335 | +text.bind("<Button-1>",gold) |
| 336 | +text.place(x=350,y=180) |
| 337 | + |
| 338 | +text =Label(root, text="SILVER",font = ("Arial", 14, "bold")) |
| 339 | + |
| 340 | +text.bind("<Enter>", partial(color_config, text, "red")) |
| 341 | +text.bind("<Leave>", partial(color_config, text, "blue")) |
| 342 | +text.pack() |
| 343 | +text.bind("<Button-1>",silver) |
| 344 | +text.place(x=350,y=210) |
| 345 | + |
| 346 | +#################################################################################################### |
| 347 | + |
| 348 | +#TEMPERATURE CONVERTER |
| 349 | +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) |
| 350 | +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) |
| 351 | +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) |
| 352 | +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) |
| 353 | +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) |
| 354 | + |
| 355 | +root.mainloop() |
0 commit comments