Hook
Build multi-platform apps in Python
More breakout videos from this creator.
Here is how to build a modern looking currency converter with a GUI in Python using the Flet library and a live exchange rate API. In this video you will learn how to fetch live exchange rates with the requests library, build a simple function to convert between currencies, and wrap it all in a modern Flet user interface with text fields and a button. Let me know if you want the source code. I could share it. First, we start by importing requests for the API calls and Flet as ft. API_URL = "https://api.exchangerate-api.com/v4/latest/USD" Then we define our convert currency function. It takes three things: the amount, the from currency, and the to currency. Inside, we send a get request to the API and pull out the rates dictionary from the response. If either currency isn't in the rates dictionary, we raise a value error with a link to the supported currencies. Otherwise, we first convert the amount to USD, and then from USD to the target currency and return the converted amount. Now for the actual GUI. def main(page: ft.Page): page.title = "Currency Converter" page.window_width = 320 page.window_height = 380 page.padding = 20 We set the title, window size, and add some padding. Next we create three text fields: One for the amount with a default value of 1, one for the from currency, defaulting to USD, and one for the to currency, defaulting to EUR. result_text = ft.Text( value="", size=20, weight=ft.FontWeight.BOLD ) We also create a result text element that starts out empty. This is where we'll show the converted amount. def convert_click(e): try: amount = float(amount_input.value) from_currency = from_input.value.upper() to_currency = to_input.value.upper() result = convert_currency(amount, from_currency, to_currency) result_text.value = f"{amount:.2f} {from_currency} = {result:.2f} {to_currency}" except Exception as ex: result_text.value = f"Error: {ex}" page.update() Then we write the convert click function. This runs whenever the button is clicked. Inside a try block, we grab the amount as a float and the from and to currencies, converting them to uppercase. We call our convert currency function with these values and build the result string. If anything goes wrong, like a typo in the currency code, we catch the exception and show the error message instead. At the end, we call page.update() to refresh the screen. Finally, we add everything to the page. page.add( ft.Column( controls=[ ft.Text("Currency Converter", size=24, weight=ft.FontWeight.BOLD), amount_input, from_input, to_input, ft.ElevatedButton(content="Convert", on_click=convert_click, width=280), result_text, ], spacing=15, ) ) We add a column with the title, the text fields, the convert button, and the result text. We give it some spacing so it doesn't look cramped. At the very bottom, we call ft.run(main) and pass it our main function to start the app. And that's it. Run it and you get a clean little desktop window. Let's test it. I'll convert 100 USD to EUR. And there we go, instant result from the API. Let's convert 125 EUR to USD. And it works. What if we try to convert to an unsupported currency? Error: https://www.exchangerate-api.com/docs/supported-currencies And we get the supported error.