Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
C++CodeDelphi

Quickly Learn How To SubClass A Python Type Created In Delphi With This Windows Sample App

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.

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.

demo26 1466202
<strong>Python4Delphi Demo26<strong>

Note: To print the DelphiPoint.X and DelphiPoint.Y values, include print(spam.myPoint) in the script and execute.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES