Want to build a Delphi/C++ builder Application that should exchange values between Delphi and Python Objects. Not sure how to do it? Python4Delphi has two powerful components to achieve this faster, you can use either one component for your application. In this post, we can see both ways.
- Python4Delphi Demo16 Example1 App shows how to assign a variable value between Delphi and Python using the TPythonDelphiVar component events. This can be achieved by events that set and gets data as a python object. Internally this component converts a value from Delphi to python and vice versa. You can find the Demo16 Example1 source on GitHub.
- Python4Delphi Demo16 Example2 App shows how to assign a variable value between Delphi and Python using TPythonModule, create a module, add few routines to that module, Import the module in a python script, and access the added routine. You can find the Demo16 Example2 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 Demo16 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.
Example1: TPythonDelphiVar Inherited from TEngineClient, used to convert the python variable to the Delphi variable and vice versa. It has methods to set and get value as variant or PyObject. It contains property like Module(TPythonModule internally created by default) where the python variable(TPyVar) is created and later converted to and from the Delphi variant or PyObject.
You can find the Python4Delphi Demo16a sample project from the extracted repository ..Python4DelphiDemosDemo16Example1Demo16a.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 in TPythonEngine DllName property. It Is assigned with InitScript which import sys module and prints the Python.Dll version, copyright information to Memo2. import sys print(“Python Dll: “, sys.version) print(sys.copyright) print()
- PythonGUIInputOutput1 provides a conduit for routing input and output between the Graphical User Interface (GUI) and the currently
executing Python script. - PythonDelphiVar1 component is intended for demonstrating how to assign value between Delphi components( like edit box, check box and group boxes) ->Python Delphi variable(Properties.Value) and back from (Properties.Value) -> Delphi components( like edit box, check box and group boxes) using event PythonDelphiVar1ExtGetData and PythonDelphiVar1ExtSetData respectively. The data is passed as Python Dictionary Type Object here. The CreateProperties and UpdateProperties routine creates a python dictionary object and Updates the python dictionary object when the value is changed in the UI.
- Memo1, used for providing the Python Script to execute, and Memo2 for showing the output.
- On Clicking Execute Button the python script is executed.
Example2: TPythonModule inherited from TMethodsContainer class allows creating modules by providing a name. You can use routines AddDelphiMethod, AddDelphiMethodWithKW to add a method of type PyCFunction. You can create events using the Events property.
You can find the Python4Delphi Demo16b sample project from the extracted repository ..Python4DelphiDemosDemo16Example2Demo16b.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 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. - PythonModule1 with Module name props is created. During PythonModule1Initialization a three DelphiMethods(GetProperty,SetProperty,GetPropertyList) are added to the Module. Logically a python module is created and its methods were created using this PythonModule. Later this can be imported into your python script wherever necessary.
- Memo1, used for providing the Python Script to execute, and Memo2 for showing the output.
- On Clicking Execute Button the python script is executed.
Head over and check out the Python4Delphi project to easily build Python GUIs for Windows.