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:
- TPythonEngine: A collection of relatively low-level routines for communicating with Python, creating Python types in Delphi, etc. It’s a singleton class.
- TPythonModule: It’s inherited from TMethodsContainer class allows creating modules by providing a name. You can use routines AddMethod, AddMethodWithKW, AddDelphiMethod, AddDelphiMethodWithKeywords to add a method which should be type compatible with this routine parameter. You can create events using the Events property.
- TPythonType: This component helps to create a new python type in Delphi which is inherited from the hierarchy of classes (set of APIs to create, manage methods, and members).
Components created in Python4Delphi Demo34 Sample App using GUI:
- 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.
- 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.
- TComboBox to list the Registered Python versions.
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:
- PythonGUIInputOutput1 provides a conduit for routing input and output between the Graphical User Interface (GUI) and the currently
executing Python script. - To retrieve the registered Python Versions, it uses unit PythonVersions routine GetRegisteredPythonVersions.
In this Sample App, PythonEngine1, PythonModule1, PythonType1 were created dynamically when Registered Python Version is selected. The PythonVersion is assigned to PythonEngine1.
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.