AndroidCodeDelphiDelphiFMXIDELearn PythonProjectsPythonPython GUI

How To Create A Calculator App Using Python GUI?

How To Create A Calculator App Using Python GUI

Python is a fantastic programming language for developing a wide range of useful scripts and programs, including graphical user interfaces (GUIs). A graphical user interface (GUI) allows users to perform operations on a program, platform, device, or application without typing commands or understanding its code. So, if you want to learn to make stunning GUIs with Python, this article is for you.

In this tutorial, we will take a look at the basics of Python GUI. We will learn how to create a simple calculator app for Windows and Android devices and take a look at some of the best Python GUI tools on the market.

What are useful tools for creating a Python GUI?

How To Create A Calculator App Using Python GUI a laptop with unleash the power of your dreams page on it

There are several tools for Python GUI that Embarcadero offers, including Delphi Community Edition, DelphiFMX, DelphiVCL, PythonFMXBuilder, Delphi4PythonExporter, and PyScripter.

What is DelphiFMX?

DelphiFMX is a natively compiled Python package using the Python4Delphi library. It’s open source and gives Python developers access to Delphi’s FireMonkey (FMX) GUI framework. GUI development with DelphiFMX is possible for many platforms, including Windows, macOS, Linux, and Android.

Embarcadero Delphi’s FireMonkey is a graphical user interface framework for developing native cross-platform applications. It makes use of the GPU via DirectX or OpenGL for hardware-accelerated rendering. It includes a robust styling system and is easily customizable by the user. DelphiFMX for Python requires no prior knowledge of Delphi or Object Pascal.

What is Delphi4PythonExporter?

Delphi4PythonExporter is a critical offering for a thriving Python and Delphi ecosystem. Python developers can save time writing design code in Python by leveraging Delphi’s flagship UI design features. As you design, they can use a WYSIWYG preview, including styles.

Furthermore, users can leverage the features during the design process to tie event methods to various events on the GUI application (for example, OnClick). Finally, Pythoneers can employ this method to scale their large-scale Python GUI application.

Delphi developers can also profit from the Exporter. They can leverage their Delphi expertise to design fantastic apps and then give Python implementations of those applications, providing you with new possibilities and an entirely new dimension.

What is PythonFMXBuilder?

The PythonFMXBuilder is a Python application builder that targets Android applications using DelphiFMX for Python. This enables you to embed a custom Python script in an Android app, deploy it to your phone, and submit it to the app market.

How can I create a simple calculator app using Python and DelphiFMX?

How To Create A Calculator App Using Python GUI A close up of a laptop with the Get RAD Studio Now webpage showing on it

Let’s create a basic calculator application for both Android and desktop devices!

What are the requirements to create a calculator app with Python?

Python, PythonFMXBuilder, Delphi4PythonExporter, Delphi, and any text editor or IDE (PyScripter is recommended) that supports Python are required for this tutorial. If you don’t have these tools installed, check out this article on Python GUI Project Setup to get started.

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

How To Create A Calculator App Using Python GUI The main Python GUI setup hero image

How to start a basic form screen for a Python App in Delphi?

Open Delphi CE and create a blank application by navigating to File > New > Multi-Device Application > Blank Application > Ok. Here we have named our project Calculator.

How To Create A Calculator App Using Python GUI A screenshot of the IDE in action

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

How To Create A Calculator App Using Python GUI Get the free Python GUI book

Next, let’s rename our app by right clicking on the form and selecting QuickEdit. Here we will name our form as CalculatorGUI with the caption Calculator Application.

How To Create A Calculator App Using Python GUI Naming the Python GUI
How To Create A Calculator App Using Python GUI Choosing a TLabel from the control pallette
How To Create A Calculator App Using Python GUI A screen with two labels on it

It is important even to name the labels so that it is easier to track them. We even need to add buttons, TButton, that, when clicked, will give appropriate results to the calculation.

How To Create A Calculator App Using Python GUI The object hierarchy
13 9835266

Next, rename the text of each label by right-clicking on that label and selecting Quick Edit.

How To Create A Calculator App Using Python GUI Selecting quick edit
How To Create A Calculator App Using Python GUI Changing the results label

Next, add some color to the form’s background. We can do this by navigating to the Object Inspector and selecting the form’s fill property. After these changes, our form will look much more visually attractive:

How To Create A Calculator App Using Python GUI Picking some colors


We can also check out how our app looks on both Desktop and Android apps. To check this, we can change the Style from Windows to Android. This will change the appearance of our form.

How To Create A Calculator App Using Python GUI Select Android as the target
How To Create A Calculator App Using Python GUI The calculator GUI

Now double-click on all the buttons on the form. Double-clicking helps to create click methods for each button in the .pas that corresponds to our .fmx form. This helps add a click functionality to each of these buttons later. Next, to each of the button Click methods add at least a single comment (//) which will ensure the method remains as we save the file in Python. There is no need to write any run-time implementation code because we don’t export the run-time implementation using the Delphi4PythonExporter.

How To Create A Calculator App Using Python GUI The empty events in code

How to export a form screen as a Python Script?

You can save your project as a Python script by selecting Tools > Export To Python > Export Current Entire Project.

How To Create A Calculator App Using Python GUI Exporting the project to Python

Delphi will generate two Python scripts, which you can rename to Calculator.py and CalculatorMain.py, in your chosen directory. Along with the Python file is a .pyfmx form that contains all the form’s visual information.

The code in CalculatorMain.py is as follows:

Now, let’s look at Calculator.py, where we will be writing the logic of our application:

How to run the calculator as a desktop App using the exported Python script?

As you remember, in the code above, all the Click methods like DelClick were left empty. Therefore, we first need to add functionality to all these buttons. So let’s start coding.

First, navigate to your project folder and open up Calculator.py:

How To Create A Calculator App Using Python GUI Calculator app in the Python IDE

Here, you will see all the objects you’ve created and the click methods for each button. For this calculator, we will use the Python eval() function that evaluates any strings that are passed onto it. So create an empty string self.Calculation in the __init__ method of the form. In addition, since the labels in our calculator contain some text, we will also initialize them as empty:

How To Create A Calculator App Using Python GUI The events

Now, we can start coding our click methods. Navigate to each click method and add the relevant character to the self.Calculation string. In addition to this, since we have created a Calc label to display the calculation, add the character to Self.Calc.Text:

How To Create A Calculator App Using Python GUI Click events

Like the C and Del functions in most calculators, our calculator does the same. If the Del key is pressed our calculator deletes the last character added, and if the C button is pressed our calculator clears all the calculations. Now, we can code the equals button method. The method uses a try and except block to try and calculate the given expression, using eval, if it is possible. If the added string is correct it shows the result in the self.Results Label, otherwise it shows an "Error" message.

How To Create A Calculator App Using Python GUI Filling out the click events

Here is what our final Calculator.py file looks like:

Finally, open up CalculatorMain.py file and run your application:

How To Create A Calculator App Using Python GUI The calculator GUI

Let’s go ahead and test this app. If everything works perfectly then you will be able to run any kind of equation on your application. For example, you can add two numbers easily, and pressing the = sign yields a result:

How To Create A Calculator App Using Python GUI The equals button on the GUI

However, if your mathematical expression to be calculated is invalid you are prompted with an error message:

How To Create A Calculator App Using Python GUI An error message on display

How to build the calculator as an Android Application using Python and PythonFMXBuilder?

The Delphi4Python Exporter exports the calculator application with initialization logic for a desktop-based application. Therefore, we will be using the PythonFMXBuilder to convert our calculator to an Android app. But before we export our calculator, we need to make some important changes to our code. 

Firstly, Android applications are Delphi applications that are initialized automatically. Therefore we don’t need to initialize the application explicitly or to set a title because it will be initialized automatically. So first lets create a new Python file named CalculatorMain_Android.py and paste the contents of CalculatorMain.py in this new file. Next, we don’t need to set the Application.MainForm, but need a variable for the Form. Lastly, just like initialization, we don’t need to run nor destroy the application as it is done automatically by Android. 

Here is what our CalculatorMain_Android.py looks like:

How To Create A Calculator App Using Python GUI The script

Save the file with the changes you’ve made. Next, Open up FMXBuilder and click on Project > New project on the toolbar. Enter the name of the project that you want to build here.

How To Create A Calculator App Using Python GUI Create a new project

Add your Python files as well as any other supporting project files you used to build your application. We will not import CalculatorMain.py as its only purpose is to run our desktop application:

How To Create A Calculator App Using Python GUI The Android project

Next, right-click on CalculatorMain_Andriod.py file and set it to the main file.

We are now ready to convert our app to Android. PythonFMXBuilder gives developers three options for building applications: build, deploy, and run. You can learn more about these options by reading PythonGUI’s e-book.

How To Create A Calculator App Using Python GUI The Android project in the editor

PythonFMXBuilder allows you to directly install the application on your mobile device. For now, we will build an Android application using a .apk file. To build your file Build project button on the toolbar.

32 5710816

When the .apk is fully generated you will be prompted with a "Build process done" message in the Messages box. You can then check out your generated file in the pathtoPythonFMXBuilderGUIexeFolderappsCalculatorbin folder.

How To Create A Calculator App Using Python GUI Folder listing showing the APK location

Simply copy and paste this .apk file into your phone’s storage. You will be prompted to install this file if you access it from your phone and click on it. Once completed, your app calculator will be available for use in the same way that any other Android app is.

Here is what our calculator app looks like:

How To Create A Calculator App Using Python GUI The calculator app running on a mobile device

Similar to our desktop app, you can make all kinds of calculations expected from a simple calculator:

How To Create A Calculator App Using Python GUI The actual calculator app

Also, similar to our desktop app, trying to evaluate an invalid calculation yields an error:

How To Create A Calculator App Using Python GUI The running calculator app

Are you prepared to create more Apps using Python and DelphiFMX?

How To Create A Calculator App Using Python GUI A laptop showing the the download PyScripter page

Congratulations on making it to the end of this tutorial! Hopefully, it has made you realize how simple it is to build Python GUIs with the right toolkit.

In this tutorial, we saw how building applications is efficient with Delphi. We learned that DelphiFMX is an extremely capable application with numerous capabilities and customization options. It is highly recommended for Windows, Linux, and Mac application developers due to its inherent support for hardware acceleration. DelphiFMX is perfect for building not just calculators, but also other useful applications such as currency converters.

If you want to learn more about Python GUI’s features, you can download the Python GUI getting started e-book. The best part about this package is that you can create multi-device applications. In addition, we also saw how using DelphiFMX library in PyScripter makes Python programming even more fun and easy.

Now that you have seen how the right set of Python GUI tools can be used to create Android and desktop applications, click here and install Delphi to start creating Python-based apps right away!

What are the FAQs regarding creating a Python GUI app?

How To Create A Calculator App Using Python GUI A person looking at a laptop screen which is on the FAQ page

What is Embarcadero?

Embarcadero is the parent company of numerous B2B productivity software companies that enable technical users to complete more tasks in less time.

Embarcadero products are designed for the most skilled programmers in the world who build and support the most important applications. Embarcadero is the developer champion for many clients, supporting them in creating corporate applications that are more secure and scalable faster than with any other solution available. Ninety percent of the Fortune 100 and a global user base of more than three million have trusted Embarcadero’s award-winning products for more than 30 years.

Why is PyScripter a user-favorite IDE?

PyScripter is a free, open-source Python IDE for Windows. Delphi’s Object Pascal and Python were used to create it.

It started as a simple IDE to provide a powerful scripting solution for Delphi applications, which gradually evolved into a full-featured stand-alone Python IDE. PyScripter 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.

PyScripter can be used as a syntax highlighting editor, a Python interpreter and debugger, a file explorer, a project manager, and more. In addition, it can be used for creating simple programs as well as more sophisticated APIs and TUIs.

What is Delphi?

Delphi is a high-level programming language that supports an object-oriented paradigm. It is a Rapid Application Development (RAD) paradigm used to create applications ranging from database solutions to mobile applications. Delphi is now widely regarded as the quickest way to develop, compile, package, and deploy cross-platform native applications for Windows, macOS, iOS, Android, and Linux.

Can you build GUIs in Python?

Creating a simple graphical user interface (GUI) that works across multiple platforms can be difficult. But this does not have to be the case. Python and Embarcadero’s DelphiFMX and DelphiVCL packages can be used to create visually appealing user interfaces for you and your users!

What is a multi-device application?

A multi-device application can operate on any platform supported by RAD Studio; multi-device apps are not restricted to desktop or mobile platforms. Multi-device applications use two of RAD Studio’s three core libraries: FireMonkey and RTL.

Related posts
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?

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Weather App With The Python Delphi Ecosystem and Weatherstack API

Leave a Reply

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