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

Flexible Way To Set And Get Variant Array Between Delphi and Python For Building Windows Apps

Do you need to exchange a variant array between Delphi and Python Quickly? Python4Delphi offers the TPythonModule component which has a method and procedure to exchange a variant array. This post guides you to do that. You can also use Python4Delphi with C++Builder.

Python4Delphi Demo17  App shows how to assign and get a variant array value using the TPythonModule component. You can find the Demo17  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.
  • TPythonModule inherited from TMethodsContainer class allows creating modules by providing a name. You can use routines SetVarFromVariant, GetVarAsVariant to set, and get a variant. You can create events using the Events property.
  • 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.

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 the name test is created which is later imported and displays the variant array generated from a python sequence. You can use routines SetVarFromVariant, GetVarAsVariant to set, and get a variant. Internally this component converts a Delphi Variant array value to a python object and vice versa.
  • On Clicking the Execute Script button, the variant comArray is created and set the variant array to PythonModule1. Later using python script written in memo1 executes and prints the output in memo2.
demo17 2067881
<strong>Python4Delphi Demo17<strong>

Its that simple passing Delphi variant array to python and get the value back from python.

Head over and find out more about combining Python and Delphi for building high performance Windows apps!

See What's New in 12.2 Athens See What's New in 12.2 Athens Dev Days of Summer 2-24

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

1 Comment

Leave a Reply

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

IN THE ARTICLES