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.
Table of Contents
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).
In the dialog box that opens, select Blank Application and click OK.
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 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.
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.
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.
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.
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.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
from delphifmx import * from TestUnit import TestForm def main(): Application.Initialize() Application.Title = 'MyTestProject' Application.MainForm = TestForm(Application) Application.MainForm.Show() Application.Run() Application.MainForm.Destroy() if __name__ == '__main__': main() |
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:
1 2 3 4 |
Application.Title = 'MyTestProject' Application.MainForm = TestForm(Application) Application.MainForm.Show() |
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.
The generated TestUnit
.py file code looks like the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import os from delphifmx import * class TestForm(Form): def __init__(self, owner): self.taskEdit = None self.Label1 = None self.btnAdd = None self.btnDelete = None self.listBox = None self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "TestUnit.pyfmx")) def btnAddClick(self, Sender): pass def btnDeleteClick(self, Sender): pass |
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 = ''
.
1 2 3 4 5 |
def btnAddClick(self, Sender): if self.taskEdit.Text != '': self.listBox.Items.Add(self.taskEdit.Text); 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)
.
1 2 3 4 5 |
def btnDeleteClick(self, Sender): index = self.listBox.ItemIndex if index >= 0: self.listBox.Items.Delete(index) |
The modified
code looks like the following:TestUnit.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import os from delphifmx import * class TestForm(Form): def __init__(self, owner): self.taskEdit = None self.Label1 = None self.btnAdd = None self.btnDelete = None self.listBox = None self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "TestUnit.pyfmx")) def btnAddClick(self, Sender): if self.taskEdit.Text != '': self.listBox.Items.Add(self.taskEdit.Text); self.taskEdit.Text = ''; def btnDeleteClick(self, Sender): index = self.listBox.ItemIndex if index >= 0: self.listBox.Items.Delete(index) |
Now we can run and test the created application by running the MyTestProject.py
file.
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.