![1867761 | Embarcadero RAD Studio Delphi C++Builder Blogs](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2020/08/1867761-scaled.jpg?resize=1140%2C694&ssl=1)
Earlier we have learned how to create a new Python Type using Delphi classes. How about inheriting Python Type created in Delphi using a simple python script? This post will guide you to do that using Python4Delphi Sample App. You can also use Python4Delphi with C++Builder.
Python4Delphi Demo26 Sample App shows how to create a Module, Python type, Import the module, and Python Type in a python script, subclass the created Python Type and use it. You can find the Demo26 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 Demo26 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 Demo26 sample project from the extracted repository ..Python4DelphiDemosDemo26.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.
- 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.
- PythonModule1 with Module name spam is created.
- On Clicking Execute Button the below code is executed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
procedure TForm1.Button1Click(Sender: TObject); var DelphiPoint : TPyPoint; p : PPyObject; begin p := PythonType1.CreateInstance; PythonEngine1.CheckError; DelphiPoint := TPyPoint( PythonToDelphi(p) ); DelphiPoint.X:=10; DelphiPoint.Y:=20; PythonModule1.SetVar( 'myPoint', p ); PythonEngine1.Py_DecRef(p); PythonEngine1.ExecStrings( memo1.Lines ); end; |
Which in turn executes the below python script mentioned in memo1. The script has a class definition MyPoint which is subclassed from a Python Type Name (Point) created in Delphi.
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
import spam class MyPoint(spam.Point): def Foo(Self, v): Self.OffsetBy(v, v) # old way to create a type instance p = spam.CreatePoint(2, 5) print (p, type(p)) p.OffsetBy( 3, 3 ) print (p.x, p.y) print ("Name =", p.Name) p.Name = 'Hello world!' print ("Name =", p.Name) # new way to create a type instance p = spam.Point(2, 5) # no need to use CreateXXX anymore print (p, type(p)) p.OffsetBy( 3, 3 ) print (p.x, p.y) # create a subtype instance p = MyPoint(2, 5) print (p, type(p)) p.OffsetBy( 3, 3 ) print (p.x, p.y) p.Foo( 4 ) 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: # this 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: # this 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") |
![Demo261466202 | Embarcadero RAD Studio Delphi C++Builder Blogs demo26 1466202](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2020/11/Demo26-1466202.png?resize=750%2C406&ssl=1)
Note: To print the DelphiPoint.X and DelphiPoint.Y values, include print(spam.myPoint) in the script and execute.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition