Skip to content

Instantly share code, notes, and snippets.

@dbarrerap
Created February 1, 2020 20:41
Show Gist options
  • Save dbarrerap/b294f79c0105d35a4466e00c0bda8b97 to your computer and use it in GitHub Desktop.
Save dbarrerap/b294f79c0105d35a4466e00c0bda8b97 to your computer and use it in GitHub Desktop.
# Convertidor de Moneda usando Python
# Destrezas:
# - Llamado de APIs
# - Crear una Interfaz Grafica
import tkinter as tk
class Converter:
def __init__(self, master):
frame = tk.Frame(master)
frame.pack()
lbl_monto = tk.Label(frame, text='Ingrese monto a convertir (ej. 4.09)')
lbl_monto.pack()
txt_monto = tk.Entry(frame)
txt_monto.pack()
btn_convertir = tk.Button(frame, text='Convertir', command=lambda: self.convertir(txt_monto.get()))
btn_convertir.pack()
self.txt_rates = tk.Text(frame, height=45)
self.txt_rates.pack(fill=tk.X)
def convertir(self, monto): # Al pedirlo del Entry, viene como texto
import requests
try:
monto = float(monto)
self.txt_rates.delete(1.0, tk.END)
res = requests.get('https://api.exchangeratesapi.io/latest?base=USD')
rates = res.json()['rates']
for currency, rate in rates.items():
# print('USD {:.2f} -> {} {:.2f}'.format(monto, currency, monto * rate))
self.txt_rates.insert(tk.END, 'USD {:.2f} -> {} {:.2f}\n'.format(monto, currency, monto * rate))
except ValueError as e:
print('Monto invalido.\n{}'.format(e))
if __name__ == "__main__":
app = tk.Tk()
root = Converter(app)
app.geometry("400x400")
app.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment