Learn How to Run A Simple Python Script In Delphi Application Using Easy Python4Delphi Sample App
How about combining the strength of Delphi and Python for your applications to provide first-class solutions for your customer needs? Thinking about how to do it? Don’t worry! Python4Delphi does for us. Python for Delphi (P4D) is a set of free components that wrap up the Python DLL into Delphi and C++Builder. They let you easily execute Python scripts, create new Python modules and new Python types. This post will guide you on how to use these components, create a VCL application, run a simple python script in it, and gets the output.
Python4Delphi Demo1 Sample App shows how to run a Python Script by typing the python code in a Memo, execute and populate the result in another Memo. You can find the Demo1 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 Demo1 App:
1.TPythonEngine: A collection of relatively low-level routines for communicating with Python, creating Python types in Delphi, etc. It’s a singleton class. Some of the key properties for the component listed here.
- AutoLoad: Enable this property to load the python DLL automatically. It defaults to true.
- AutoUnload: Enable this property to unload the python DLL automatically. It defaults to true.
- DllName: By default, it reflects the latest e.g. python39.dll. But you can provide your registered version.
- DllPath: Physical location of the Python DLL.
- InitScript: You can provide the python script here to execute implicitly.
- IO: Associate the TPythonGUIInputOutput component to this property.
- RegVersion: Registered python version e.g 3.9.
- UseLastKnownVersion: If you want to use the latest registered Python version installed set this property to True.
2.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.
- MaxLineLength: Maximum character length per line.
- MaxLines: Maximum python script lines can be executed using this component.
- OutPut: Associate the Memo component to show the output.
- RawOutput: Produce the result as raw output.
- UniCodeIO: Set true to provide Input and output as Unicode.
3.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 Demo1 sample project from the extracted GitHub repository ..Python4DelphiDemosDemo01.dproj. Open this project in RAD Studio 10.4.1 and run the application.
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. - Memo1 used for providing the Python Script and Memo2 for showing the output. Buttons to perform the execution, load script from, and save the script to file.
- PythonGUIInputOutput1‘s property Output to Memo2.
- On Clicking Execute Button the script strings are executed using the below code.
PythonEngine1.ExecStrings( Memo1.Lines); |
Load the script to the Memo1 from a file using the code
with OpenDialog1 do begin if Execute then Memo1.Lines.LoadFromFile( FileName ); end; |
Save the Script to the file using the code,
with SaveDialog1 do begin if Execute then Memo1.Lines.SaveToFile( FileName ); end; |
Check some of the tutorials available for Python4Delphi here