Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

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.

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.

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:

[crayon-676a0a9c24cc5480186979/]

Load the script to the Memo1 from a file using the code

[crayon-676a0a9c24ccc077491625/]

Save the Script to the file using the code,

[crayon-676a0a9c24ccd571831565/]
Python4Delphi Demo1

Check some of the tutorials available for Python4Delphi here  

Learn what the lightning staff code like low-code platforms offers to its users and how user-friendly its drag-and-drop interface is.

Exit mobile version