AndroidCodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How to Create a Unit Converter Android App Using Python GUI

How to Create a Unit Converter Android App Using Python GUI

Mobile applications have become increasingly popular today, where technology plays a significant role in our lives. One such application is a unit converter app, which allows users to convert different units of measurement quickly and easily. If you’re interested in learning how to create a unit converter app for Android using Delphi For Python tools, then you’re in the right place. This tutorial will cover creating the UI, writing conversion logic, and packaging the app for Android devices. The result is a fully functional app that users can share. So let’s get started!

Why Create a Unit Conversion App?

How to Create a Unit Converter Android App Using Python GUI Laptop with the VCL and FMX libraries for Python on the screen

A unit conversion app can be a helpful tool for a wide range of day-to-day tasks that require converting between different units of measurement. For example, when cooking, a recipe may call for certain ingredients in metric units, but the user may be more familiar with using imperial units. In this case, a unit conversion app can quickly convert the measurements to the preferred unit system, ensuring the recipe is prepared accurately.

So, unit conversion apps make it easy to convert between different units of measurement. Instead of manually calculating conversions, users can simply input a value and select the desired unit of measurement. Moreover, unit conversion apps help ensure accuracy by eliminating the potential for human error in manual calculations.

A dedicated app can save time and increase efficiency for tasks involving frequent unit conversions, such as engineering or scientific calculations. Also, by creating your own unit conversion app, you can tailor it to your specific needs and preferences, including which units to include, the user interface, and any additional features

Why Create Python GUI Using Delphi For Python?

How to Create a Unit Converter Android App Using Python GUI An mage of a person editing a Python GUI app on their laptop

Delphi for Python is a package that allows Python developers to use Delphi components and libraries to create GUI applications. With Delphi for Python, Python developers can leverage the power of Delphi VCL and FMX frameworks to create native Windows applications or cross-platform applications, respectively. The package also includes Delphi4PythonExporter, which can export Python modules to Delphi DLLs.

PythonFMXBuilder is another tool that can be used to create cross-platform GUI applications using Python and Delphi FMX. It allows developers to build and deploy Python GUI applications to Windows, macOS, iOS, and Android platforms.

How to Create a Unit Converter Desktop App?

How to Create a Unit Converter Android App Using Python GUI The RAD Studio IDE

Before we start with the tutorial, let’s look at its prerequisites.

What Are the Requirements to Complete this Task?

The latest version of Python, PythonFMXBuilder, Delphi4PythonExporter, Delph CE, and any text editor or IDE (PyScripter is recommended) that supports Python are required for this tutorial.

We’ve got you covered if you don’t have these installed on your device. We recommend checking out this article, “Powerful Python GUI Project Setup” which will help you get started.

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

How to Create a Form in Delphi?

Open Delphi CE and go create a blank application (File > New > Multi-Device Application > Blank Application > Ok). This will open a new project for you with a blank form. We have named our project UnitConversionApp.

How to Create a Unit Converter Android App Using Python GUI a blank form in the RAD Studio IDE

To understand what each section of the above Delphi IDE means/represents, please go through the free eBook bundle we have developed. This eBook explains the ideology around Delphi Python EcoSystem, all Python GUI offerings, and much more.

How to Create a Unit Converter Android App Using Python GUI the Python EBook download image

Let’s start by giving our form a name. Right-click on the form and click on QuickEdit. Here we will name our form Main and display the title as Unit Conversion App.

How to Create a Unit Converter Android App Using Python GUI Naming the Python GUI app

Next, let’s resize our form. Head to the object inspector, and in the properties tab, search for ClientHeight and ClientWidth. We will set them as 480 and 306, respectively.

How to Create a Unit Converter Android App Using Python GUI The Object Inspector

We will also rename the source file of our main form to MainForm.pas to make it easy to keep track of.

How to Create a Unit Converter Android App Using Python GUI the project manager

This form will be the primary interface for our Unit Converter application, where users will choose the units to convert. We’ll add a label that greets users with the app’s title to make it more like a main screen. To do this, navigate to the standard palette and locate the TLabel component. Afterward, simply drag and drop the component onto the form.

How to Create a Unit Converter Android App Using Python GUI Searching the component palette for a TLabel

Now, let’s change the text style to display the title. We can easily do this using TextSettings. So, select the Tlabel and head over to the Object Inspector on the bottom-left corner of your screen. Here in the search bar, search for TextSettings. Select Font under it and click on the ... to see options allowing you to change the label font, size, and style according to your needs.

How to Create a Unit Converter Android App Using Python GUI changing some of the control's properties in the object inspector.

Now rename the label’s text by right-clicking on that label and selecting Quick Edit. It is good practice to follow an intuitive naming convention to name your components to make them easier to track. Here, we named our label as Title with the text “Unit Conversion App.

How to Create a Unit Converter Android App Using Python GUI The app's GUI

We can change the form’s background color by navigating to the Object Inspector and selecting the form’s fill property.

How to Create a Unit Converter Android App Using Python GUI Chaning the fill style
How to Create a Unit Converter Android App Using Python GUI the brush designer

Finally, we need to add a few buttons that will help us navigate to other forms where we will be converting our Units. So head on over to the Palette and add a few TButton components.

How to Create a Unit Converter Android App Using Python GUI searching the component palette for a TButton
How to Create a Unit Converter Android App Using Python GUI the basic form

As you can see, we have added four buttons to our form named Length, Temperature, Time, and Weight. These buttons will help us navigate to different forms that will help us convert the relevant units.

Next, let’s change the font size and style of this button text to make the button text more prominent. Here we have to use the font style as Segoe UI and increase the size to 16pt. We also increased the size of the buttons and aligned the text to the Center, the same as the Title label above, so that the text appears symmetric.

How to Create a Unit Converter Android App Using Python GUI the object inspector

Here is what our final MainForm looks like:

How to Create a Unit Converter Android App Using Python GUI a preview of the unit conversion app

Now that we’ve created our main screen, let’s add four new forms to help us with the unit conversions. To create our next form, right-click on UnitConversionApp.exe and select Add New > Multi-Device Form.

How to Create a Unit Converter Android App Using Python GUI selecting to add a new multi device form

This will create our new form. We will rename this form as Length and the source file as LengthForm.pas.

Next, resize the form to the same specifications as our MainForm to maintain consistency. Let’s also add a few labels and buttons to our form and make it more visually appealing.

Here is what our form looks like:

How to Create a Unit Converter Android App Using Python GUI the GUI

As you can see, we have also added a small Status label indicated by the hyphen right above the Back and Convert buttons. We have also changed its color using TextSettings to help us display error messages based on wrong user inputs.

Now that our LengthForm has the basic visual components let’s add a few components that will allow us to convert our lengths based on user input. Start by navigating to the Palette and searching for the TComboBox component. We will add this to the form for the user to select the conversion unit. In addition to this, we will add a label under the Value label to indicate the result of the conversion.

How to Create a Unit Converter Android App Using Python GUI adding additional features to the GUI

Finally, let’s add a TEdit component that will help the user write the unit’s value to be converted. Here is what our final LengthForm looks like:

How to Create a Unit Converter Android App Using Python GUI the final details

Now that we know how to create a unit form, we can create similar forms for Temperature, Time, and Weights. Here is what our final forms look like:

19 4566276
20 9007786
21 2680062

Now that our forms are ready, we need to add procedures to ensure our buttons work. So open up each form by double-clicking on the .fmx file of each form.

How to Create a Unit Converter Android App Using Python GUI

Next, double-click each button to construct the Click methods in the .pas files of our forms.

How to Create a Unit Converter Android App Using Python GUI

To ensure that the methods are not lost when we export our forms in Python, add at least a single comment (//)to each of the Click methods.

How to Create a Unit Converter Android App Using Python GUI

How to Export this Delphi Project as a Python Script?

Now that our forms are ready, we can export our project as a Python script. Navigate to Tools > Export To Python > Export Current Entire Project.

How to Create a Unit Converter Android App Using Python GUI

Next, give your application a title, select the Application Main Form as the MainForm, and, finally, select the directory of choice. Click on Export to generate your files.

How to Create a Unit Converter Android App Using Python GUI exporting the UI

Because we have 5 forms for this project, Delphi will generate 6 Python scripts i.e., MainForm.py, LengthForm.py, TimeForm.py, TemperatureForm.py, WeightForm.py, and UnitConversionApp.py in your chosen directory. The form Python files contain the classes for the individual forms, and the UnitConversionApp is used to launch our Delphi app. 

Along with the Python files for each form, Delphi generates a .pyfmx file for each .py file in the same directory. These files contain all the form’s visual information.
Let’s take a look at the contents of UnitConversionApp.py, which will run our application:

Next, let’s take a look at MainForm.py that will show us our main screen:

Here are the contents for the rest of the forms.

LengthForm.py:

TemperatureForm.py:

TimeForm.py:

And finally WeightForm.py:

How to Add Functionality to the App?

Now that our forms are ready, we can add functionality to our App.

For our MainForm the only thing we need to do is link the relevant button to each of the forms. To do this, let’s import the conversion forms into our MainForm.py:

Next, navigate to the Click function for each form, create an instance of the form and assign it to a variable. We then display the new form using the show() method. For example, let’s navigate to TemperatureFromButtonClick, create an instance of TemperatureForm, and assign it to a new variable self.TempForm. We then use show() to display our form. Here is what our TemperatureFromButtonClick looks like:

We can replicate this for the other buttons. Here is what our final MainForm.py file looks like:

Now, let’s move our LengthForm. First, let’s start by defining a dictionary in the __init__ function that stores the length conversions for each unit.

Here we have defined the dictionary such that to convert from meter to kilometer, we use the meter key to get the nested dictionary and then access the kilometer conversion using that dictionary. Here we get the number 1/1000, so multiplying any user value with this number will yield our converted result. 

Next, let’s iterate over the dictionary and populate the combo boxes in our form.

Finally, we initialize the Status label empty using the .Text property of the labels. Here is what our final __init__ function looks like:

Next, let’s redefine the Back button to open up our new form. So in the BackButtonClick method,  use the show() method to open up the MainForm and then we will use the .Destroy() method to destroy the current form, then .

Here is what our BackButtonClick method looks like:

Finally, lets define our ConvertClick function that will make our conversion happen. First, start by checking whether the user has selected values in both combo boxes and added a value to be converted. Next, use the .Text property to get the given and to units from the form. We also get the amountGiven by the user in the edit box, and finally calculate the amount by simply multiplying the amountGiven with the conversion rate from the dictionary:

But before we can display our value, we add a check to ensure that the calculated value does not exceed 8 characters. If it does, we then convert it into scientific notation. We then reset the Status and display our converted value on the label.

Finally, if the user fails to fill in any required details we will display an error in the Status label. Here is what our final ConvertClick function looks like:

Following is the final LengthForm.py file code:

We can follow the same process to add functionalities to our WeightForm, and TimeForm. Here is that our final WeightForm.py looks like:

And here is what our final TimeForm.py looks like:

However, it is important to note that unlike our other forms, the temperature conversions require us to use formulae that involve operations other than division and multiplication. So let’s make changes to our TemperatureForm.py.

What Changes Have to be Made in the Temperature Form? 

Start by opening TemperatureForm.py and define the __init__ functions and BackClick button like our other forms.

Next, we will need to redefine our dictionary to store our functions. So navigate to the __init__ function and create a lambda function for each unit conversion.

Let’s take a look at an example, to convert from Celsius to Kelvin, we need to multiply our Celsius value by 9/5 then add 32 to it. We thus create a lambda function that takes in a single value and converts it into Celsius as lambda a: a * (9.5) + 32. We can replicate the same for other conversions. Therefore, to convert a value, we will use the dictionary to get the relevant function and then pass in the value to be converted. For example, self.data["celsius"]["fahrenheit"](5) will convert 5 degree Celsius to Fahrenheit.

Next, we will change the ConvertClick function to pass values to the lambda functions to calculate the relevant amounts. Here is what our final TemperatureForm.py looks like:

What Results Does Testing This Desktop App Give?

Now that code is ready, we can run our app by executing UnitConversionApp.py. Here is what MainForm looks like:

How to Create a Unit Converter Android App Using Python GUI the UI in action

We can even navigate to the different forms by clicking each button. Here is what our LengthForm looks like:

How to Create a Unit Converter Android App Using Python GUI the length form

Here is what our TimeForm:

How to Create a Unit Converter Android App Using Python GUI another form

Here is our TemperatureForm:

How to Create a Unit Converter Android App Using Python GUI the temperature form

And finally here is what our WeightForm looks like:

How to Create a Unit Converter Android App Using Python GUI 0 the weight form

How To Turn This Into an Android App?

How to Create a Unit Converter Android App Using Python GUI a laptop showing the PythonFMXBuilder download page

Now that our Unit Conversion App is running, let’s convert this into an Android application.

So, open up FMXBuilder and click on Project > New project on the toolbar. Enter the name of the project that you want to build here. Here we define our project as UnitConversionApp.

How to Create a Unit Converter Android App Using Python GUI naming the project

Next, add your Python and .pyfmx files by right-clicking on the App and selecting Add file.

How to Create a Unit Converter Android App Using Python GUI adding a file

Before we can add our UnitConversionApp.py file, we need to change some code to allow us to run our project in Android. Let’s create a new file named UnitConversionApp_Android.py and paste the contents of UnitConversionApp.py.

Next, open up the UnitConversionApp_Android.py file and start making some important changes. First off, Android applications are initialized automatically by Delphi applications. Because the program will be initialized automatically, we don’t need to initialize it or set a title manually. The Application.MainForm doesn’t need to be initialized either but just needs a variable. Lastly, like initialization, we don’t need to launch or destroy the program because Android handles both tasks.

Here is what our UnitConversionApp_Android.py file looks like:

Save the file with the code changes you’ve just made. Now, right-click on UnitConversionApp_Android.py file and set it to the main file.

How to Create a Unit Converter Android App Using Python GUI setting the main form

PythonFMX Builder for Android can generate an android application package file (.apk) or install the app directly onto your Android device. To directly install the app onto your mobile device, you’ll first need to connect your Android phone to your device and enable USB debugging on your phone. Afterward, simply click on the reload button located in the top right corner. If your device is properly connected, it will appear in the drop-down menu.

How to Create a Unit Converter Android App Using Python GUI the APK details

Finally, select your device and click on Run Project:

36 9884589

This will install the app directly on your Android phone and run it.

Does the Android App Work As We Expect?

Upon opening the app on your Android device, you will see the main menu:

How to Create a Unit Converter Android App Using Python GUI testing the Android app

Now, if we select the length option, we will be taken to the LengthForm where we can select our desired units and do successful conversions:

How to Create a Unit Converter Android App Using Python GUI the length

We can even check whether the resultant conversion is given in scientific notation when required.

How to Create a Unit Converter Android App Using Python GUI more on the length form

Let’s also check that if we had not filled in the required details and pressed the Convert button, do we get an error:

How to Create a Unit Converter Android App Using Python GUI more details

We can even check that our TemperatureForm, TimeForm, and WeightForm work as expected:

How to Create a Unit Converter Android App Using Python GUI temp form
How to Create a Unit Converter Android App Using Python GUI the time form
How to Create a Unit Converter Android App Using Python GUI the weight form

Are You Ready To Create Your Own Unit Converter App?

How to Create a Unit Converter Android App Using Python GUI a laptop showing the Edge browser description page for RAD Studio 11

Congratulations on completing this tutorial on creating an Android unit converter app using Python GUI! We hope this tutorial has given you insights into how Python can be used to develop mobile apps and simplify the creation of graphical user interfaces. With the help of Delphi for Python Exporter and PythonFMXBuilder, you can easily deploy Python scripts on Android devices and create visually appealing UIs in Delphi. The Delphi Community Edition is a great tool with a streamlined IDE, code editor, integrated debugger, and hundreds of visual components.

Now that you have the necessary tools, you can create Python-based mobile apps using Delphi. So, why not get started and create your app today? The possibilities are endless!

What Are Some FAQs Related to this Tutorial?

How to Create a Unit Converter Android App Using Python GUI the FAQs

What is Embarcadero?

Embarcadero technologies are created for the best-skilled programmers worldwide who build and support the most important applications. Because we are the developer champion and can help them create scalable, secure enterprise apps more quickly than with any other solution on the market, our clients select Embarcadero. Ninety of the Fortune 100 and a vibrant community of more than three million customers worldwide have relied on Embarcadero’s award-winning products for more than 30 years.

What GUI library is used to create the app’s user interface?

The app’s user interface is created using the delphifmx library and other tools such as Delphi CE, PythonFMXBuilder, and Delphi4PythonExporter.

Is it possible to create an Android app using Python?

Yes, it is possible to create an Android app using Python.

Do I need any prior programming experience to follow this tutorial?

While some programming experience would be helpful, this tutorial is designed to be beginner-friendly and walks you through the process step-by-step.

Related posts
CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And NumPy Library?

CodeIDEProjectsPythonWindows

Unlock the Power of Python for Deep Learning with Generative Adversarial Networks (GANs) - The Engine behind DALL-E

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Matplotlib Library?

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Pillow Library?

Leave a Reply

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