DelphiFMXPythonPython GUI

How To Create A Currency Converter Python GUI App With DelphiFMX

How To Create A Currency Converter Python GUI App With DelphiFMX

A graphical user interface, more commonly known as a GUI, is an important component of software application programming for human-computer interaction, replacing text-based commands with user-interactive visual actions. With the aid of a GUI, users can perform operations on a device, platform, program, or application without having to type commands or comprehend the underlying coding. So, if you want to learn how to create stunning GUI applications using Python, you’ve come to the right place! In this article, we will look at how to build a simple currency conversion application using some of the best Python GUI tools on the market.

What is DelphiFMX?

DelphiFMX is a natively compiled Python module powered by the Python4Delphi library. It’s free to use and distribute, and it gives Python developers access to Delphi’s mature and rich FireMonkey GUI framework, which provides a simple way to create GUIs compatible across all platforms.

FireMonkey by Embarcadero Delphi is a GUI framework for native cross-platform application development that uses hardware-accelerated rendering via DirectX or OpenGL. It also has a strong styling system and is user extensible.

How To Create A Currency Converter Python GUI App With DelphiFMX the Delphi FMX screen

How can you create a currency conversion Python GUI application using DelphiFMX?

Let’s create a basic GUI application where a user can provide a currency name and an amount in that currency. Our application would output the exchange amount in any desired currency.

How To Create A Currency Converter Python GUI App With DelphiFMX a laptop showing a Python script

What are the requirements to complete the Currency Conversion Python GUI application?

For this tutorial, you will need Python and any text editor or an IDE that supports Python development (PyScripter is recommended). In addition to this, you will need to install the DelphiFMX package. You will also need a basic understanding of Python and DelphiFMX.

You can install the DelphiFMX Python package via PyPi:

You can grab the code for this tutorial from our git repository at: github.com/Embarcadero/PythonBlogExamples/tree/main/Currency_Converter_GUI

How to create a simple UI application?

How To Create A Currency Converter Python GUI App With DelphiFMX example code screenshot 1

Let’s get acquainted with some basic functions of this library to progress with our currency converter app. For this, we can create a basic DelphiFMX application simpleGUI.py that displays a short message:

Running this code would display the following:

How To Create A Currency Converter Python GUI App With DelphiFMX a blank Python GUI screen

To get a detailed understanding of the above code, please go through the free eBook bundle that we developed. This eBook explains the ideology around Delphi-Python EcoSystem, Python GUI offerings, and much more.

How To Create A Currency Converter Python GUI App With DelphiFMX a link asking you to download a free Python GUI ebook

How to enhance a Python GUI to create a currency converter?

Let’s create a new Python file CurrencyConverter.py and write code in it. Our code will use two libraries:

How To Create A Currency Converter Python GUI App With DelphiFMX example code screenshot 2

Now, we will define a class CurrencyConvert that will define our main form:

Here we are making a simple form with eight labels using the Label class of delphifmx. Some of these labels are being used to display information regarding components in our application and guide the user better. With that said, the primary label of interest is self.A2Value, which we will use to display the converted amount from a given currency to the desired currency. Moreover, we also create a two combo boxes self.comboC1 and self.comboC2 that will display all available currencies that are available in the API in the form of a dropdown menu. These comboxes drop-down list will get populated with all of the country codes using the __load_country_codes method. In addition, we create a single textbox using the Edit class that we will use to define the input amount to be converted.

Next, we define a simple Button object self.convert with text, Convert, which when clicked, will call the __button_click_convert method of our class. For now, we leave the method empty.

How To Create A Currency Converter Python GUI App With DelphiFMX example code screenshot 3

Now, we will define our main method that initializes the CurrencyConvert form that we defined earlier:

Adding the following lines will initiate the app whenever this Python code file is run:

Running the code will give us this GUI:

How To Create A Currency Converter Python GUI App With DelphiFMX the Currency Converter screen

How to add functionality to the convert button in our Python GUI?

Earlier, we had left the __button_click_convert method as empty. Now let us redefine this function such that it can display the converted currency amount on the form.

How To Create A Currency Converter Python GUI App With DelphiFMX example code screenshot 4

For our application, we can use a currency exchange API to convert currencies, which in our case is APILayer’s Exchange Rates Data API.

Here is how we define our button click method:

In this function, we will first have to retrieve the values input by the user. To do this, we call the Text property of the Selected property of our ComboBox class to get the currency code for conversion. We store this in our local variable named given (which stores the current currency) and to (which specifies the currency to be converted in). Then, using the Text property of the Edit class, we get the amount of the current currency and store it in the variable amountGiven

Next, we define our exchange rate API. To do this, we first create a free account on APILayer’s Exchange Rates Data API. The variable headers is where we specify our API key. Please enter the generated API key for you to an “apikey” key value of the headers dictionary. Finally, we can retrieve our currency amount by calling the API using a GET request. Finally, we store the response json in a result variable and display the converted current value on our A2Value label.

How do we initialize our currencies using the APILayer API in Python?

As mentioned previously we will be using the __load_country_codes method to initialize our currencies by populating our combo boxes. Here is how we define the method:

When the function __load_country_codes is called in the __init__ method, it simply calls the Exchange Rates Data API and retrieves all the currencies supported by the API. Once all the information has been retrieved, it extracts all the symbols from the response and provides them in drop-down list of the combo boxes. We are making use of the append() method of the Items property (attribute) of the ComboBox class to add items into it.

Why should you use APILayer’s Exchange Rates Data API in Python?

The Exchange Rates Data API is a real-time, intraday, and historical market data API. With over 15 exchange rate data sources, the Exchange rates API is a powerful and robust RESTful API that delivers exchange rate data for more than 170 world currencies.

How To Create A Currency Converter Python GUI App With DelphiFMX setting up the APILayer details

This API contains many endpoints, each of which serves a distinct purpose or use case. For example, receiving the most recent exchange rate information for a specific set or all currencies; converting one currency to another; receiving data time-series for one or more currencies; preserving the API daily for fluctuation data are among the endpoints.So your final CurrencyConverter.py should look like this:

How can we test our Currency Converter Python GUI app?

Now let us test the functionality of our code. First, run the application and start populating the combo boxes and the text boxes. Here we will try to convert US Dollar – $1 to Euros equivalent:

How To Create A Currency Converter Python GUI App With DelphiFMX the Python GUI screen for the currency converter in action

If everything is set up correctly, then you should simply click on the Convert button, and this will convert the given USD $1 amount into Euros as per the exchange rates at the moment you clicked on Convert button using the API:

How To Create A Currency Converter Python GUI App With DelphiFMX the results of currency conversion using the APILayer API in a Python GUI

In addition, since we are using APILayer’s Exchange Rates API to get real-time data of 170+ currencies, we should also be able to convert other currencies. So add the Canadian Dollar “CAD” and amount to the From field and do the same for Omani Rial “OMR” in the To field. Let’s say we want to know the equivalent of 213 CAD in OMR. Clicking Convert gives us the following result:

How To Create A Currency Converter Python GUI App With DelphiFMX more example screen shots

Why should Python developers use PyScripter to create Python GUIs?

How To Create A Currency Converter Python GUI App With DelphiFMX the PyScripter download page shown on a laptop screen

PyScripter is a Python-integrated development environment (IDE) for Windows that is free and open source. It was created using Delphi’s Object Pascal and Python.

PyScripter started as a simple IDE to provide a powerful scripting solution for Delphi applications. It gradually evolved into a full-featured stand-alone Python IDE. It is written in Delphi and can be extended with Python scripts using Python4Delphi (P4D). It is relatively light compared to other IDEs because it is written in a compiled language. In addition, it is a portable IDE that can be used as a syntax highlighting editor, a Python interpreter and debugger, a file explorer, and a project manager, among other things.

What have you learned in this Python Currency Converter GUI application tutorial?

How To Create A Currency Converter Python GUI App With DelphiFMX a person with their hands on the keyboard of a laptop ready to download PyScripter

Congratulations on finishing this tutorial! Hopefully, this has opened your eyes to how simple it is to build Python GUIs with the right toolkit. In addition, we’ve seen how important Python bindings for Delphi GUI libraries are for the Python GUI developer community.

A user interface does not have to be difficult to design. Pip packages give you access to a wide range of Python tools. DelphiFMX is an extremely capable application with numerous capabilities and customization options. It is highly recommended for Windows, Linux and Mac desktop application developers due to its inherent support for hardware acceleration. If you want to learn more about Python GUI’s features, you can download the Python GUI getting started e-book.

How To Create A Currency Converter Python GUI App With DelphiFMX an image for someone to click to download a free e book

In addition, we also saw how using PyScipter while using the DelphiFMX library makes Python programming even more fun and easy.

Now that you know all about PyScripter click here and create your own application using your favorite IDE. 

What are the FAQs about creating a Python GUI Currency Converter app?

How To Create A Currency Converter Python GUI App With DelphiFMX a laptop showing the PyScripter help page

What is Delphi?

Delphi is an extremely powerful yet easy to learn object-oriented programming language. It has a Rapid Application Development (RAD) paradigm that can be used to create anything from database solutions to mobile applications. It is noted for the ease of deployment of Delphi apps – which do not require any additional runtimes or dependencies, along with its robustness and single-source cross-platform capabilities. As a result, Delphi is widely regarded as the most efficient way to create, compile, package, and deploy cross-platform native applications for Windows, macOS, iOS, Android, and Linux. 

Products and tools like Delphi Community Edition, Python4Delphi, DelphiFMX, DelphiVCL, PyScripter, Delphi4PythonExporter, and PythonFMXBuilder allow Python developers to create all kinds of amazing professional quality applications very easily.

What other applications can you create using PyScripter and DelphiFMX?

With DelphiFMX, you are limited only by your imagination. Delphi, along with PyScripter, provides you with powerful tools that you can use to create powerful, versatile applications, including, but not limited to, calculators, alarm clocks, stopwatches, and even simple games.

What is Embarcadero?

Embarcadero is an Idera, Inc. subsidiary. Idera, Inc. is the parent company of global B2B software productivity brands whose solutions allow technical users to do more in less time.

Embarcadero creates tools intended for the world’s most elite developers who create and maintain the most critical applications. Embarcadero is the developer champion for our customers, assisting them in developing secure and scalable enterprise applications that are faster than any other tool on the market. Embarcadero’s award-winning products have been relied on by ninety percent of the Fortune 100 and a global community of more than three million users for over 30 years.

What is DelphiVCL?

DelphiVCL is a Visual Component Library that supports Delphi’s state-of-the-art user interface. These include a majority of native Windows controls, as well as controls with enhanced features and functionality. If you wish to develop applications only for Windows platform, you can take advantage of DelphiVCL.

What is APILayer?

APILayer is a leading provider of off-the-shelf, cloud-based API products that help developers and businesses all over the world run more efficiently and effectively. Organizations of all sizes are struggling to support complex digital transformations, as well as the growing demand for connectivity and real-time data. APILayer does exactly that!

Related posts
DelphiFMXDelphiVCLIDELearn PythonPythonPython GUI

How To Get Started With Python GUI Development?

Learn PythonPython

How To Make A Python Script For Solving MP2 Equations

DelphiFMXDelphiVCLPythonPython GUI

How To Make a Powerful Delphi Python GUI Project Setup

C++CodePythonPython GUIWindows

What Is The Best Python Graph Tool? graph-tool vs NetworkX

Leave a Reply

Your email address will not be published. Required fields are marked *

en_USEnglish