Earlier we learned how to create a new Python Type for a Delphi Record using the TPythonType component’s property TheType. It’s also necessary to learn how to use Delphi classes for creating a new Python Type using Python4Delphi component’s property PyObjectClass. You can also use Python4Delphi with C++Builder. This post will help to understand that.
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 Demo8 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.
- 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 Demo8 sample project from the extracted repository ..\Python4Delphi\Demos\Demo08.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. - In this sample, Created a Delphi class(TPyPoint) implementing a new Python type. It is derived from TPyObject. It overrides some methods, like the constructors, the RegisterMethods, and the type services’ virtual methods. During PythonType1 initialization assign the property PyObjectClass with the class TPyPoint.
1 2 3 4 |
procedure TForm1.PythonType1Initialization(Sender: TObject); begin PythonType1.PyObjectClass := TPyPoint; end; |
- PythonModule1 with Module name spam is created.
- On Clicking Execute Button the below script is executed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import spam p = spam.CreatePoint(2, 5) print (p) p.OffsetBy( 3, 3 ) print (p.x, p.y) print (dir(spam)) print (spam.Point) print ("p = ", p, " --> ",) if type(p) is spam.Point: print ("p is a Point") else: print ("p is not a point") p = 2 print ("p = ", p, " --> ",) if type(p) is spam.Point: print ("p is a Point") else: print ("p is not a point") p = spam.CreatePoint(2, 5) try: print ("raising an error of class EBadPoint") p.RaiseError() # it will raise spam.EBadPoint except spam.PointError as what: #it shows you that you can intercept a parent class print ("Caught an error derived from PointError") print ("Error class = ", what.__class__, " a =", what.a, " b =", what.b, " c =", what.c) # You can raise errors from a Python script too! print ("------------------------------------------------------------------") print ("Errors in a Python script") try: raise spam.EBadPoint("this is a test !") except: pass try: err = spam.EBadPoint() err.a = 1 err.b = 2 err.c = 3 raise err except spam.PointError as what: #it shows you that you can intercept a parent class print ("Caught an error derived from PointError") print ("Error class = ", what.__class__, " a =", what.a, " b =", what.b, " c =", what.c) if p == spam.CreatePoint(2, 5): print ("Equal") else: print ("Not equal") |
Check some of the tutorials available for Python4Delphi here
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition