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

Learn How To Dynamically Create And Destroy Python4Delphi Components With A Delphi Windows App

Earlier we have learned how to create a Python Type using Python4Delphi components in a Delphi GUI app. From the post we know, how to create and use Python4Delphi components such as TPythonEngine, TPythonModule, TPythonType by a simple drag and drop in our application. But sometimes developers may need to instantiate those components dynamically rather than using GUI components. How to do that? This post will guide you to understand better. You can also use Python4Delphi with C++Builder. 

Python4Delphi Demo34 Sample App shows how to create, destroy, recreate PythonEngine, and other Python4Delphi components in a Delphi application. You can find the Demo34 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 created Dynamically in Python4Delphi Demo34 Sample App:

Components created in Python4Delphi Demo34 Sample App using GUI:

You can find the Python4Delphi Demo34 sample project from the extracted repository ..Python4DelphiDemosDemo34.dproj. Open this project in RAD Studio 10.4.1 and run the application.

Implementation Details:

[crayon-6767ec1e160cb563866629/]

In this Sample App, PythonEngine1, PythonModule1, PythonType1 were created dynamically when Registered Python Version is selected. The PythonVersion is assigned to PythonEngine1.

[crayon-6767ec1e160d2743377269/]

After creating the necessary components, Upon clicking the Execute button, PythonType1 created an instance of its type, added to PythonModule variable myPoint. Later the python script in Memo1.lines is executed.

[crayon-6767ec1e160d3601998767/]
<strong>Python4Delphi Demo34<strong>

Exit mobile version