How about combining the strength of Delphi and Python for your applications to provide first-class solutions for your customer needs? Or you are an expert in Delphi who want to also work with Python, as it is a high and growing demand in the market out there?
Thinking about how to do it? Don’t worry! Python4Delphi does for us. Python for Delphi (P4D) is a set of free components that wrap up the Python DLL into Delphi and C++Builder. They let you easily execute Python scripts, create new Python modules and new Python types. This post will guide you on how to use these components, create a VCL application, run a simple Python Hello World! script in it, and gets the output.
You can easily build Python GUI apps using your favorite Python language features and libraries for Windows using Delphi and C++Builder and Python4Delphi. In order to run the Python script in Python for Delphi, open and run project Demo01
. Then insert the script into lower Memo
, click Execute script
button, and get the result in upper Memo
. You can find the Demo01
source on GitHub.
Python4Delphi Demo01
Sample App shows how to run a Python Script by typing the python code in a Memo
, execute and populate the result in another Memo
. You can find the Demo01
source on GitHub.
Prerequisites: Download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out this video Getting started with Python4Delphi.
First, open the Demo01.dproj
with RAD Studio:
Table of Contents
Components used in Python4Delphi Demo01
App:
1. TPythonEngine
A collection of relatively low-level routines for communicating with Python, creating Python types in Delphi, etc. It’s a singleton class. Some of the key properties for the component listed here.
AutoLoad
: Enable this property to load the pythonDLL
automatically. It defaults toTrue
.AutoUnload
: Enable this property to unload the pythonDLL
automatically. It defaults toTrue
.DllName
: By default, it reflects the latest e.g.python39.dll
. But you can provide your registered version.DllPath
: Physical location of the PythonDLL
.InitScript
: You can provide the python script here to execute implicitly.IO
: Associate theTPythonGUIInputOutput
component to this property.RegVersion
: Registered python version e.g3.9
.UseLastKnownVersion
: If you want to use the latest registered Python version installed, set this property toTrue
.
2. TPythonGUIInputOutput
Inherited from TPythonInputOutput
(which works as a console for python outputs). Using this component Output
property you can associate the Memo
component to show the output.
MaxLineLength
: Maximum character length per line.MaxLines
: Maximum python script lines can be executed using this component.OutPut
: Associate theMemo
component to show the output.RawOutput
: Produce the result as raw output.UniCodeIO
: Set true to provide Input and output as Unicode.
3. TMemo
A multiline text editing control, providing text scrolling. The text in the memo control can be edited as a whole or line by line.
You can find the Python4Delphi Demo01
sample project from the extracted GitHub repository ../python4delphi/Demos/Demo01/Demo01.dproj. Open this project in RAD Studio 10.4.1 and run the application.
Implementation Details
PythonEngine1
provides the connection to Python or rather the Python API. This project uses Python3.9 which can be seen inTPythonEngine
DllName
property.PythonGUIInputOutput1
provides a conduit for routing input and output between the Graphical User Interface (GUI) and the currently
executing Python script.Memo1
is used for providing the Python Script andMemo2
for showing the output.Buttons
to perform the execution, load script from, and save the script to file.PythonGUIInputOutput1
‘s propertyOutput
toMemo2
.- On Clicking
Execute script
Button the script strings are executed using the below code:
1 2 3 4 |
procedure TForm1.Button1Click(Sender: TObject); begin PythonEngine1.ExecStrings( Memo1.Lines ); end; |
- Load the script to the
Memo1
from a file using the below code:
1 2 3 4 5 6 7 8 |
procedure TForm1.Button2Click(Sender: TObject); begin with OpenDialog1 do begin if Execute then Memo1.Lines.LoadFromFile( FileName ); end; end; |
- Save the script to the file using the below code:
1 2 3 4 5 6 7 8 |
procedure TForm1.Button3Click(Sender: TObject); begin with SaveDialog1 do begin if Execute then Memo1.Lines.SaveToFile( FileName ); end; end; |
- And then, click “
Run Without Debugging (Shift+Ctrl+F9)
“:
- Type and run “
Hello World!
” and Python Basic Mathematical Operations to see it in actions:
1 2 3 4 5 6 7 8 9 |
print("Hello World!") print("Hello Python4Delphi!") print(2+2) print(2-2) print(2*2) print(2/2) print(2%2) print(2**2) print(2//2) |
Congratulations, now you can successfully run Hello World Python GUI In A Delphi Windows App! Which as you may know that even “Hello World!” is often the first program written by people learning to code, it can also be used as a sanity test to make sure that a computer language is correctly installed and that the operator understands how to use it.
From now, the applications are endless and the only limit is your imagination.
Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.
Is it a Delphi version python IDE?
It’s a bridge between the RAD Studio Delphi and Python as well as an IDE which allows close integration between the power of Delphi applications and the rich features of Python. It’s a great combination. https://sourceforge.net/projects/pyscripter/