Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!

Ultra-Fast Way To Wrap Delphi Objects To Python Objects With Python4Delphi Sample App

How to wrap the existing or new Delphi Objects into Python Objects? Not sure? Python4Delphi has the flexibility to do that using a TPyDelphiWrapper component. This post will guide you on how to wrap a Delphi Object to a python Object. You can also use Python4Delphi with C++Builder.

Python4Delphi Demo32 Sample App shows how to wrap a Delphi Object to Python Object with some Examples as listed. You can find the Demo32 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 Demo32 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.
  • TPythonModule: It’s inherited from TMethodsContainer class allows creating modules by providing a name. You can use routines AddMethod, AddMethodWithKW to add a method of type PyCFunction. You can create events using the Events property.
  • TPyDelphiWrapper: Component that wraps the Delphi Object to Python Object inherited from TEngineClient. It has the capability to store Delphi class registration information, registration for Helper Types(not correspond to Delphi Classes), register python module functions, created event handlers. Wrap, WrapRecord, WrapInterface are the key methods to wrap Delphi Object, Record, and Interface respectively.
  • 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 Demo32 sample project from the extracted GitHub repository ..Python4DelphiDemosDemo32.dproj. Open this project in RAD Studio 10.4.1 and run the application.

Implementation Details:

  • PythonEngine component provides the connection to Python or rather the Python API. This project uses Python3.9 which can be seen in the TPythonEngine DllName property.
  • PythonGUIInputOutput component provides a conduit for routing input and output between the Graphical User Interface (GUI) and the currently
    executing Python script.
  • PyDelphiWrapper component contains Module and Engine Property which is associated with name spam and PythonEngine1 respectively.

A Delphi class TPoint is created, which is wrapped by a wrapper class TPyPoint The PyDephiWrapper initialize the TPyPoint by below procedure.

In this sample, On clicking Execute button, it is wrapped to Python Object using the following code.

Followed by wrapping the TPoint, the below memo1 Python scripts demonstrate the type and representation.

demo32 5067913
<strong>Python4Delphi Demo32<strong>

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.