We have learned how to create a Python Module in Delphi and added some methods to it. It’s time to learn how to create a Python Type(Class) in Delphi. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state. Class instances can also have methods (defined by its class) for modifying its state. You can also use Python4Delphi with C++Builder. This post will guide you to create a Python Type in Delphi using Python4Delphi components.
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 Demo6 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.
- 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.
- TPythonDelphiVar: Inherited from TEngineClient, used to convert the python variable to the Delphi variable and vice versa. It has methods to set and get value as variant or PyObject. It contains property like Module(TPythonModule internally created by default) where the python variable(TPyVar) is created and later converted to and from the Delphi variant or PyObject.
- 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).
- 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 Demo6 sample project from the extracted repository ..Python4DelphiDemosDemo06.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. It Is assigned with InitScript which import sys module and prints the Python.Dll version, copyright information to Memo2. import sys print(“Python Dll: “, sys.version) print(sys.copyright) print()
- PythonGUIInputOutput1 provides a conduit for routing input and output between the Graphical User Interface (GUI) and the currently executing Python script.
- PythonDelphiVar1 component is intended for demonstrating how to assign value between Delphi(Edit1.Text)->Python Delphi variable(test.Value) and back from (test.Value) -> Edit1.Text using event PythonDelphiVar1GetData and PythonDelphiVar1SetData respectively. The data is passed as variant type here.
- PythonModule1 with Module name spam is created. During PythonModule1Initialization a method spam_foo,spam_CreatePoint,spam_getdouble is added to the Module. and the definition of these methods was included in the same unit file. In the method definition, we can use the python engine to manipulate between python and Delphi.
- In this sample, created a new Python Type(Point) which is a Delphi record that holds attributes Ob_refcount, X, Y positions, and a Pointer to TPyTypeObject. This Python type point is instantiated using the spam_CreatePoint routine. To get and set the attribute value for the created point instance, procedure PyPoint_getattr, PyPoint_setattrfunc is used. To represent the Point object procedure PyPoint_repr is used. To destroy the Point object PyPoint_dealloc is used. All these Python Point type routines should be defined as PythonType1 PyTypeObject attributes during initialization as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
procedure TForm1.PythonType1Initialization(Sender: TObject); Var PyType : PyTypeObject; begin with (Sender as TPythonType) do begin // In the initialization of a new type, we must // define the attributes of this type PyType := TheType; with PyType do begin tp_basicsize := sizeof(PyPointRec); tp_dealloc := PyPoint_dealloc; tp_getattr := PyPoint_getattr; tp_setattr := PyPoint_setattrfunc; tp_repr := PyPoint_repr; tp_str := PyPoint_repr; end; TheType := PyType; // And then add the methods of the object, if needed AddMethod( 'OffsetBy', PyPoint_OffsetBy, 'OffsetBy(dx, dy)' ); end; end; |
- Memo1, used for providing the Python Script which imports spam (PythonModule1), accesses the method spam_foo, creates a Point type using spam_CreatePoint, perform object manipulations using routines created, and shows the output. Buttons to perform the execution, load script from, and save the script to file.
- On Clicking Execute Script Button the script,
Check some of the tutorials available for Python4Delphi here