Python 3 Tkinter Script to Build a Realtime Currency Converter Using REST API GUI Desktop App Full Project For Beginners

Python 3 Tkinter Script to Build a Realtime Currency Converter Using REST API GUI Desktop App Full Project For Beginners

 

Welcome folks today in this blog post we will be building a realtime currency converter in tkinter using rest api in python. All the full source code of application is shown below.

 

 

Get Started

 

 

In order to get started you need to make an app.py file and copy paste the following code

 

app.py

 

# 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()

 

READ  Python 3 Script to Find Second Largest Element in an Array or List Using Sort() Method Full Tutorial For Beginners

 

Now if you execute the python script by typing the below command as shown below

 

python app.py

 

Leave a Reply