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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
rocedure TForm1.Button1Click(Sender: TObject); procedure DisplayArrayVar( V : Variant ); var i, j : Integer; item : Variant; s : String; begin Memo2.Lines.Add('Displaying a variant array generated from a Python sequence:'); for i := VarArrayLowBound( V, 1 ) to VarArrayHighBound( V, 1 ) do begin item := V[i]; for j := VarArrayLowBound( item, 1 ) to VarArrayHighBound( item, 1 ) do begin s := item[j]; Memo2.Lines.Add(Format('(%d, %d) = %s',[i, j, s])); end; end; end; var ComArray : Variant; begin ComArray := VarArrayCreate([0, 3, 0, 2], varVariant); ComArray[0, 0] := 1; ComArray[0, 1] := 1.1; ComArray[0, 2] := 'a'; ComArray[1, 0] := 2; ComArray[1, 1] := 2.2; ComArray[1, 2] := 'b'; ComArray[2, 0] := 3; ComArray[2, 1] := 3.3; ComArray[2, 2] := 'c'; ComArray[3, 0] := 4; ComArray[3, 1] := 4.4; ComArray[3, 2] := 'd'; PythonModule1.SetVarFromVariant( 'L', ComArray ); PythonEngine1.ExecStrings(Memo1.Lines); DisplayArrayVar( PythonModule1.GetVarAsVariant( 'L' ) ); end; |
Its that simple passing Delphi variant array to python and get the value back from python.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition
After run the line “PythonModule1.SetVarFromVariant( ‘L’, ComArray )”, is executed the script in memo1.lines, the first line of memo1 is “import test”, Will all variables set using SetVarFromVariant be available in the test library?