CodeDelphiDelphiFMXPythonPython GUI

How To Write A Simple Todo Gui Application Using Delphifmx

How To Create A Calculator App Using Python GUI

A graphical user interface (GUI) is an essential part of any application that allows users to interact with the program and perform necessary actions easily. Python is a powerful popular programming language widely used to create different types of applications. However, it is intended mainly for writing backend code and generally does not have simple tools for creating a user interface. Therefore, if you need to create a GUI in Python, you should use our best Python GUI tools on the market.

This article will explain how to use the RAD Studio with Delphi Community Edition to design the application’s GUI. Then we will export created GUI using Delphi4PythonExporter and add functionality to the created application’s UI using the DelphiFMX package for Python. When we have finished, we will have created a fully working Python program with a really slick and professional GUI or graphical user interface.

What is DelphiFMX?

DelphiFMX is a Python package that provides Python developers with access to the FireMonkey (FMX) GUI framework of the Delphi programming language. It is freely redistributable and supports GUI application development for Windows, MacOS, Linux, and Android. DelphiFMX for Python does not require Delphi or any prior knowledge of Object Pascal.

What is Delphi4PythonExporter?

With the Delphi4PythonExporter extension, Python developers can use the design features of the Delphi IDE, such as drag and drop editor, preview, styles, event method binding, etc. They can use Delphi to build great programs and then offer Python implementations of them. It allows them to create an open-source GUI in Python without writing a single line of design code in Python.

How to create a TODO application using these tools?

Let’s create a simple GUI application where users can create a list of TODO tasks, add tasks to the list, and review and delete them.

What are the requirements to create this project?

To create a Python GUI TODO application using this tutorial, we need to install Delphi IDE – Delphi Comunity Edition, DelphiFMX package, and Delphi4PythonExporter.

Read the following GUI project setup article to learn how to install these tools.

How to create a project in Delphi?

First, open Delphi Community Edition and create a blank application (click File -> New -> Multi-Device Application).

How To Write A Simple Todo Gui Application Using Delphifmx creating a new project

In the dialog box that opens, select Blank Application and click OK.

How To Write A Simple Todo Gui Application Using Delphifmx creating a blank application

After that, a project with a default name will be created. We can change the name by clicking File -> Save As. Afterward, we should select the directory to save the project, enter a name, and click Save. After creating the project, a UI designer will open a form on which we can place other components. To change the form’s name, select the form, and in the Object Inspector, find the Name property and enter the desired name.

How To Write A Simple Todo Gui Application Using Delphifmx using the object inspector

How do I add visual components to my Delphi form?

Now let’s add visual components to the form. All visual components are located in the Palette panel. Expand the Standard tab and drag the TLabel, TEdit, TListBox, and two TButton components onto the form.

How To Write A Simple Todo Gui Application Using Delphifmx adding visual components to a screen form

We can change the position and size of the components using the WYSIWYG graphic designer or select the required component and change the desired property in the Object Inspector. In addition, we can also change borders, color, font, title, displayed text, and many other properties of visual components. Depending on the type of component, the properties may vary.

How To Write A Simple Todo Gui Application Using Delphifmx an example screen

After we have placed all the visual components on the form, double-click the Add task and Delete task buttons to generate procedures to handle events by clicking those buttons automatically. Add comment lines to them to preserve those empty event methods.

5 5831750

How to export the Delphi project to a Python script?

Once our project’s GUI is ready, we can export it to Python.

Click Tools -> Export to Python -> Export Entire Current Project.

How To Write A Simple Todo Gui Application Using Delphifmx exporting the project to Python

Fill in the Application Title and Application Directory in the dialog box and click Export. After that, two Python files will be created:

  • The first file contains the application initialization.
  • The second file contains form logic. Also, a form file .pyfmx will be created.
How To Write A Simple Todo Gui Application Using Delphifmx the Python export form

If the export is successful, a corresponding message will appear.

What files were created during export?

During export, two Python files should have been created: MyTestProject.py and TestUnit.py. The MyTestProject.py is the main Python file, and its code contains the following:

In the main() function, the application is initialized using the Application.Initialize() method. The application is then given a title and specifies that the main form / main window is the TestForm class from TestUnit.py file:

Then the application is run using the Application.Run() method. Finally, after the program is closed, the Application.MainForm.Destroy() method is executed, which releases all the resources occupied by the program.

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, all Python GUI offerings, and much more.

image 1412714 2407127

The generated TestUnit.py file code looks like the following:

In this file, all form components are initialized. Also, there are empty event methods btnAddClick and btnDeleteClick that handle the events of pressing buttons on the form. Since these methods are empty so far, the buttons will not work when the program is launched. To run the application GUI, you need to execute the MyTestProject.py file.

How do I add logic to my app?

Now we can add operation logic using the DelphiFMX Python package. For working with Python code, we can use the popular open-source Python IDE PyScripter.

When we click the Add button, we should check if the text field is not empty – if self.taskEdit.Text != ''. If so, the entered text will be added to the TODO list – self.listBox.Items.Add(self.taskEdit.Text) and the text field will be cleared – self.taskEdit.Text = ''.

To delete an item/task from the TODO list, select it and click the Delete Task button. In the deletion procedure, we must first determine the index of the chosen record – index = self.listBox.ItemIndex. If it is greater than or equal to 0 - if index >= 0 (which means there is a selected entry) then we delete it self.listBox.Items.Delete(index).

The modified TestUnit.py code looks like the following:

Now we can run and test the created application by running the MyTestProject.py file.

How To Write A Simple Todo Gui Application Using Delphifmx the running ToDo app

Do you want to create your own Python GUI application?

This article taught you how to easily create GUIs in Python using the best Python GUI tools. You can use the DelphiFMX Python package to create Python-based GUI applications. Also, use Delphi IDE and Delphi4PythonExporter add-ons to become more efficient and scale your applications.

Related posts
CodeIDELearn PythonPythonPython GUITkinter

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

CodeIDEProjectsPythonWindows

Unlock the Power of Python for Deep Learning with Radial Basis Function Networks (RBFNs)

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

Leave a Reply

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