Sometimes we may need to use Python objects like COM automation objects, inside your Delphi source code. Thinking about how to do it? Don’t worry. Python4Delphi has an excellent library unit that does for us. Using this we just create python objects by passing values as a variant that will return the python type as Delphi variant type. Also, the library has extensive helper routines to validate the type as well. This post guide you to understand better using the Python4Delphi sample app. You can also use Python4Delphi with C++Builder.
Python4Delphi Demo25 Sample App shows how to create a python variable type (i.e. Integer, Float, String, Dates, Mappings, Object types) in Delphi by just passing values as variant type parameters. You can find the Demo25 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.
Components used in Python4Delphi Demo25 App:
- TPythonEngine: A collection of relatively low-level routines for communicating with Python, creating Python types in Delphi, etc. It’s a singleton class.
- 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.
- 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.
- VarPyth.pas – set of classes and helper routines to create python types and return variant types in Delphi. You can assert whether the type is Python type, Kind of python types(e.g. IsInteger, IsBool, IsFloat), etc. Also, you can use BuiltinModule routines to manipulate variant values.
You can find the Python4Delphi Demo25 sample project from the extracted GitHub repository ..Python4DelphiDemosDemo25.dproj. Open this project in RAD Studio 10.4.1 and run the application.
- PythonEngine1 provides the connection to Python or rather the Python API. This project uses Python3.9 which can be seen in TPythonEngine DllName property.
- PythonGUIInputOutput1 provides a conduit for routing input and output between the Graphical User Interface (GUI) and the currently
executing Python script.
- In this sample app, We have Buttons which unit tests different python types in Delphi. This is achieved by routines in VarPyth.pas. some e.g mentioned below.
- VarPythonCreate – Create a python type in Delphi bypassing variant values as a parameter. Internally the python object type is created based on the value in the parameter and returns the variant. Using this in Delphi we can perform python arithmetic operations, string manipulations, sequence operations, etc with the help of VarPyth helper routines.
- Memo2, used for providing the Python Script to execute, and Memo1 for showing the output.
- On Clicking Execute Button the python script is executed.
- On Clicking Run Selected tests once it will validate each type created and manipulates some arithmetic, string manipulations, etc.
procedure TMain.btnTestIntegersClick(Sender: TObject);
a, b, c : Variant;
big : Int64;
// initialize the operands
a := VarPythonCreate(2);
Assert(Integer(a) = 2);
b := VarPythonCreate(3);
Assert(Integer(b) = 3);
Note : The demo sample demonstrates more functionalities within each Button clicks for different python types compared to above snippet.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition