Create a Currency Converter Using PySimpleGUI

Posted by


In this tutorial, we will be building a simple currency converter using PySimpleGUI. PySimpleGUI is a Python library that makes it easy to create graphical user interfaces (GUIs) in Python.

To get started, you will need to have Python and PySimpleGUI installed on your computer. If you don’t already have them installed, you can install them using pip by running the following command in your terminal:

pip install PySimpleGUI

Once you have PySimpleGUI installed, you can start building your currency converter.

First, create a new Python script and import the necessary libraries:

import PySimpleGUI as sg
import requests

Next, define a function that will fetch the latest exchange rates from an API. We will be using the exchangeratesapi.io API for this tutorial, but you can use any other API of your choice.

def get_exchange_rates():
    response = requests.get('https://api.exchangeratesapi.io/latest')
    data = response.json()

    return data['rates']

Now, let’s create the layout for our currency converter GUI. We will use PySimpleGUI’s layout syntax to create a simple GUI with two dropdown menus for selecting the currencies to convert between, an input field for entering the amount to convert, and a button to trigger the conversion.

layout = [
    [sg.Text('Convert from:'), sg.InputCombo(('USD', 'EUR', 'GBP', 'JPY'), key='from_currency', default_value='USD')],
    [sg.Text('Convert to:'), sg.InputCombo(('USD', 'EUR', 'GBP', 'JPY'), key='to_currency', default_value='EUR')],
    [sg.Text('Amount to convert:'), sg.Input(key='amount')],
    [sg.Button('Convert')]
]

Next, create the window for our GUI using the layout we defined:

window = sg.Window('Currency Converter', layout)

Now, let’s create the main loop for our currency converter. In this loop, we will wait for the user to interact with the GUI and handle the conversion when the Convert button is clicked.

exchange_rates = get_exchange_rates()

while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

    if event == 'Convert':
        from_currency = values['from_currency']
        to_currency = values['to_currency']
        amount = float(values['amount'])

        converted_amount = amount / exchange_rates[from_currency] * exchange_rates[to_currency]

        sg.popup(f'{amount} {from_currency} is equal to {converted_amount:.2f} {to_currency}')

window.close()

Finally, run your script and test out your currency converter. You should see a window with dropdown menus for selecting the currencies to convert between, an input field for entering the amount to convert, and a button to trigger the conversion. Enter a value to convert and press the Convert button to see the converted amount.

Congratulations! You have successfully built a currency converter using PySimpleGUI. You can further customize the GUI by adding more features or styling to suit your needs.

0 0 votes
Article Rating
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@blendex707
1 month ago

Deez