C++CodeDelphi

Quickly Transfer Variant Arrays Between Python And Your Windows GUI App Front End

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.
Python4Delphi Demo17

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!

Related posts
CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And OpenCV Library?

CodeIDEProjectsPythonWindows

Unlock the Power of Python for Deep Learning with Radial Basis Function Networks (RBFNs)

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And NumPy Library?

CodeIDEProjectsPythonWindows

Unlock the Power of Python for Deep Learning with Generative Adversarial Networks (GANs) - The Engine behind DALL-E

Leave a Reply

Your email address will not be published. Required fields are marked *