Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

Easily Pass Values Between Delphi And Python In Your Windows Delphi/C++ Builder Apps

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.

  1. 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.
  2. 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:

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:

[crayon-6767dc7737d04266481284/]
<strong>Python4Delphi Demo16a<strong>

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:

[crayon-6767dc7737d0e908900526/]
<strong>Python4Delphi Demo16b<strong>

Head over and check out the Python4Delphi project to easily build Python GUIs for Windows.

Exit mobile version