DelphiDelphiVCLLearn PythonProjectsPythonPython GUIWindows

How To Build A Currency Converter Tool Python Desktop App


Python is an excellent programming language for creating various handy scripts and programs. Today we are going to build a currency exchange converter tool. We will go through all the details necessary to understand the code, even if you are an absolute beginner.

First, download the PyScripter IDE, where you will be building the currency exchange converter Python desktop app.

What is a currency exchange converter?

A currency exchange converter is a tool used to calculate the equivalent value of one currency in terms of another. There are various currency conversion tools online where you choose two currencies, enter the amount and get the converted value.

What is the simplest Python currency exchange converter?

The most basic way of building the converter is to ask the user to enter the exchange rate and the amount to be converted. However, this is inconvenient from a user experience perspective. Let’s do it together as a warm-up exercise.

How to make the tool obtain the currency rate automatically?

There are various free Python packages to download the currency exchange rate data. In this example, we will use the forex-python package. It can be easily installed in PyScripter by going to Tools -> Tools -> Install packages with pip. In the field, you have to enter forex-python and press OK. Soon after, the package is installed, and you are ready to start using it. Below is an example of how to obtain all the exchange rates of USD to other currencies. Afterwards, we convert $10 to euro.

The package obtains the latest exchange rate, though a minor modification can also give data from the past. A datetime object can be given as an argument to the get_rate() function.

How to build a GUI for the currency exchange converter Python desktop app tool?

Building a Python desktop application is a nice challenge you can give yourself. There are several packages that allow you to create windows, menus, add buttons, lists and so on. Your imagination is the limit. We have previously reviewed some of the popular Python GUI packages.

Today we are going to use Embarcadero’s DelphiVCL4Python to build our application. There are plenty of resources in the blog, but I will briefly introduce how to construct a Python graphical interface. I will start with a note that for some platforms, there is a different capitalization of the package name. My version under Windows is written entirely in small letters, though some people have to write it as DelphiVCL.

I built the example graphical program by starting from existing examples. I borrowed the example from the GitHub page of DelphiVCL4Python. There is also a YouTube tutorial about the topic that we highly recommend checking out. I also used the nice example published on this blog.

How to actually code our Python desktop app?

How to initialize, show and exit our Python desktop app

The application is defined, initialized, started and killed in the main function. In this example, the object MainForm refers to a class called ActivityIndicatorForm.

The ActivityIndicatorForm class contains a long list of functions. Naturally, there is an initialization function and a function that creates the components. The components we will be using in this example are a list of radio buttons, an input field, a button and labels about them. The window also has a basic File menu with an Exit button, though it is optional for such a simple program since there is already the x on the top bar.

How To Build A Currency Converter Tool Python Desktop App - image of defining the Python GUI

How to define the Python app’s components

The code of the library is intuitive and self-explanatory most of the time. The only less clear definition in my opinion is the “TEdit” component, i.e., a text input field.

How to configure the properties of the Python GUI components

Each component has various parameters and properties to set up, including width, height, caption, as well as events to happen when the item is clicked. We set the window caption, and its size and initialize a menu.

The menus are added as items to the main menu we defined in the object mm_menu. Each menu has an index. Here, we only have one, hence the number 0. The second argument is the name of the submenu.

Submenu items are added in a similar way. Probably the most important of their properties is the event occurring when the entry is clicked.

Text can be added as the caption property of labels. The position is defined using the Left and Top parameters.

The text input field is also very simple to set up. There are two important points. The property Text is what will be visible in the field before the user enters any text. Here, I have put 0 as a default amount of money to convert. The property Name is a way to refer to the component from the code. I will get back to it soon.

The currencies can be added as radio buttons, for example. I could have also used a drop-down list. Each currency is added as an item to the list. In the end, we define that the first radio button is selected. It could be left out in some cases. Here it would cause the program to crash if the user clicks the Calculate button before having selected the currencies.

Similarly to the text field, the button also carries both a Caption and a Name. Obviously, it also needs an event to happen when clicking it; otherwise, it is useless. This button calls the function from this class called __convert_currency.

How to make Python perform the currency conversion

The currency conversion requires the input amount, the input currency and the output currency. The amount of money to convert is entered in the text field. Python treats it as a string, so we need to convert it to a number.

The float function verifies that the input makes sense. It gives an error if I would enter letters. The program does not crash, though, and the user can correct their mistake.

How To Build A Currency Converter Tool Python Desktop App - an image of the currency convertor showing an error due to a floating point conversion

The user provides the currencies via the radio buttons, so we need a way to read the selected value. The index of the selected radio button is stored at

This variable is a number between 0 and 4 in this example since we have defined five currencies. I can use this index to get what is the text belonging to the entry. The string is stored as an item:

I was being a bit lazy here. I could have split it in this way:

The variable selected_input_currency is a string, one of the five currencies we have defined.

More tips and explanations for our currency converting Python desktop app tool

I hope this was not too overwhelming. If you are a beginner, classes might appear too hard to grasp, but actually, they are simply a component consisting of functions and variables. The functions are not directly available to the code outside the class. This makes it smoother to integrate classes from different packages which might potentially have the same function name.

The self quantity is something often seen in object-oriented programming. This is a way of accessing the variables and functions belonging to the object or class.

Here is the complete example code for writing the currency exchange converter Python desktop app tool

Click here to download PyScripter and run the code yourself!

About author

I am a quantum chemist and a photographer with a decade of experience in programming in various languages. I have worked with python in the past five years regularly on multiple projects and in my free time. I love sharing knowledge with the world and clearly explaining complex concepts.
Related posts
IDEPythonPython GUI

What Is The Best IDE For Python?

Python GUI

What Are The Best Web Scraping Tools For Python

Python GUI

How To Create A Python Desktop Application

CodeLearn PythonPythonWindows

The Comprehensive Guide To Built-In Python Testing Tools

Deixe uma resposta

O seu endereço de e-mail não será publicado.