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.
Table of Contents
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 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.
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:
1 |
pip install delphifmx |
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?
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
from delphifmx import * Application.Initialize() Application.Title = "Delphi FMX" main_window = Form(Application) Application.MainForm = main_window main_window.SetProps(Caption = "Python4Delphi") msg = Label(main_window) msg.SetProps(Parent = main_window, Text = "Delphi FMX For Python GUIs", Position = Position(PointF(20, 20)), Width = 200) main_window.SetProps(Width = 315, Height = 400) #Window dimensions main_window.Show() Application.Run() #Main loop main_window.Destroy() |
Running this code would display the following:
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 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:
1 2 3 |
import requests from delphifmx import * |
Now, we will define a class CurrencyConvert
that will define our main form:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
class CurrencyConvert(Form): def __init__(self, owner): self.SetProps(Caption = "Currency Converter App", width=350) #Non-editable Labels self.heading = Label(self) self.heading.SetProps(Parent = self, Text = "Currency Converter", Position = Position(PointF(70, 10)), Width=200, Height=40, StyleLookup = 'listboxheaderlabel') self.fromm = Label(self) self.fromm.SetProps(Parent = self, Text = "From", Position = Position(PointF(20, 60)), StyleLookup = 'listboxitemlabel') self.to = Label(self) self.to.SetProps(Parent = self, Text = "To", Position = Position(PointF(20, 180)), StyleLookup = 'listboxitemlabel') self.C1 = Label(self) self.C1.SetProps(Parent = self, Text = "Currency: ", Position = Position(PointF(20, 90))) self.C2 = Label(self) self.C2.SetProps(Parent = self, Text = "Currency: ", Position = Position(PointF(20, 210))) self.A1 = Label(self) self.A1.SetProps(Parent = self, Text = "Amount: ", Position = Position(PointF(180, 90))) self.A2 = Label(self) self.A2.SetProps(Parent = self, Text = "Amount: ", Position = Position(PointF(180, 210))) self.A2Value = Label(self) #To display converted value self.A2Value.SetProps(Parent = self, Text = "-", Position = Position(PointF(180, 240))) #Editable textboxes self.editA1 = Edit(self) #Currency1 amount self.editA1.SetProps(Parent = self, Position = Position(PointF(180, 120)), Height = 20) #Drop-down menu for currency self.comboC1 = ComboBox(self) #Currency1 name self.comboC1.SetProps(Parent = self, Position = Position(PointF(20, 120)), Height = 20) self.comboC2 = ComboBox(self) #Currency2 name self.comboC2.SetProps(Parent = self, Position = Position(PointF(20, 240)), Height = 20) #Getting Country Codes self.__load_country_codes() #Button self.convert = Button(self) self.convert.SetProps(Parent = self, Text = "Convert", Position = Position(PointF(170, 300)), Width = 100, OnClick = self.__button_click_convert) def __load_country_codes(self): #To get country codes pass def __button_click_convert(self, sender): #Method called when convert button clicked pass |
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.
Now, we will define our main method that initializes the CurrencyConvert
form that we defined earlier:
1 2 3 4 5 6 7 8 |
def main(): Application.Initialize() Application.Title = "Currency Converter Using Delphi FMX" Application.MainForm = CurrencyConvert(Application) Application.MainForm.Show() Application.Run() Application.MainForm.Destroy() |
Adding the following lines will initiate the app whenever this Python code file is run:
1 2 3 |
if __name__ == '__main__': main() |
Running the code will give us this GUI:
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.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
def __button_click_convert(self, sender): #Method called when convert button clicked given = self.comboC1.Selected.Text to = self.comboC2.Selected.Text amountGiven = int(self.editA1.Text) url = "https://api.apilayer.com/currency_data/convert?to={}&from={}&amount={}".format(to, given, amountGiven) headers= { "apikey": "" ### Enter your Currency Data API's Unique API key in this string ### } response = requests.request("GET", url, headers=headers) #API GET request result = response.json() #Reading json response self.A2Value.Text = str(result["result"]) #Displaying the response |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
def __load_country_codes(self): url = "https://api.apilayer.com/currency_data/list" headers= { "apikey": "" ### Enter your Currency Data API's Unique API key in this string ### } response = requests.request("GET", url, headers=headers) #API GET request result = response.json() #Reading json response codes = list(result['currencies'].keys()) for code in codes: self.comboC1.Items.append(code) self.comboC2.Items.append(code) |
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.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
#Imports import requests from delphifmx import * class CurrencyConvert(Form): def __init__(self, owner): self.SetProps(Caption = "Currency Converter App", width=350) #Non-editable Labels self.heading = Label(self) self.heading.SetProps(Parent = self, Text = "Currency Converter", Position = Position(PointF(70, 10)), Width=200, Height=40, StyleLookup = 'listboxheaderlabel') self.fromm = Label(self) self.fromm.SetProps(Parent = self, Text = "From", Position = Position(PointF(20, 60)), StyleLookup = 'listboxitemlabel') self.to = Label(self) self.to.SetProps(Parent = self, Text = "To", Position = Position(PointF(20, 180)), StyleLookup = 'listboxitemlabel') self.C1 = Label(self) self.C1.SetProps(Parent = self, Text = "Currency: ", Position = Position(PointF(20, 90))) self.C2 = Label(self) self.C2.SetProps(Parent = self, Text = "Currency: ", Position = Position(PointF(20, 210))) self.A1 = Label(self) self.A1.SetProps(Parent = self, Text = "Amount: ", Position = Position(PointF(180, 90))) self.A2 = Label(self) self.A2.SetProps(Parent = self, Text = "Amount: ", Position = Position(PointF(180, 210))) self.A2Value = Label(self) #To display converted value self.A2Value.SetProps(Parent = self, Text = "-", Position = Position(PointF(180, 240))) #Editable textboxes self.editA1 = Edit(self) #Currency1 amount self.editA1.SetProps(Parent = self, Position = Position(PointF(180, 120)), Height = 20) #Drop menu for currency self.comboC1 = ComboBox(self) #Currency1 name self.comboC1.SetProps(Parent = self, Position = Position(PointF(20, 120)), Height = 20) self.comboC2 = ComboBox(self) #Currency2 name self.comboC2.SetProps(Parent = self, Position = Position(PointF(20, 240)), Height = 20) #Getting Country Codes self.__load_country_codes() #Button self.convert = Button(self) self.convert.SetProps(Parent = self, Text = "Convert", Position = Position(PointF(170, 300)), Width = 100, OnClick = self.__button_click_convert) def __load_country_codes(self): url = "https://api.apilayer.com/currency_data/list" headers= { "apikey": "" ### Enter your Currency Data API's Unique API key in this string ### } response = requests.request("GET", url, headers=headers) #API GET request result = response.json() #Reading json response codes = list(result['currencies'].keys()) for code in codes: self.comboC1.Items.append(code) self.comboC2.Items.append(code) def __button_click_convert(self, sender): #Method called when convert button clicked given = self.comboC1.Selected.Text to = self.comboC2.Selected.Text amountGiven = int(self.editA1.Text) url = "https://api.apilayer.com/currency_data/convert?to={}&from={}&amount={}".format(to, given, amountGiven) headers= { "apikey": "" ### Enter your Currency Data API's Unique API key in this string ### } response = requests.request("GET", url, headers=headers) #API GET request result = response.json() #Reading json response self.A2Value.Text = str(result["result"]) #Displaying the response def main(): Application.Initialize() Application.Title = "Currency Converter Using Delphi FMX" Application.MainForm = CurrencyConvert(Application) Application.MainForm.Show() Application.Run() Application.MainForm.Destroy() if __name__ == '__main__': main() |
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:
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:
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:
Why should Python developers use PyScripter to create Python GUIs?
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?
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.
In addition, we also saw how using PyScipter while using the DelphiFMX library makes Python programming even more fun and easy.
What are the FAQs about creating a Python GUI Currency Converter app?
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!