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

Learn To Use Events To Build Python GUI Apps For Windows With Delphi And C++

An event links an occurrence in the system with the code that responds to that occurrence. The occurrence triggers the execution of a procedure called an event handler. The event handler performs the tasks that are required in response to the occurrence. Events allow the behavior of a component to be customized at design-time or at run time. Do you want to trigger and handle an event similar to Delphi events? Python4Delphi provides a mechanism to do that. This post will guide you on how to create events using Python4Delphi Components.

Python4Delphi Demo21 Sample App shows how to create a Module, Python type, and add an event to it and define the event, Import the module and Python Type in a python script, and access the created event. You can find the Demo21 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 Demo21 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 create events using the Events property. After creating the event with a name, its handler can be defined by clicking the OnExecute method which takes the sender, PythonObject, and a variable parameter result as argument.
  • TPythonType: It’s inherited from TGetSetContainer class contains a set of APIs to create, manage a Python Type in Delphi. Similar to TPythonModule, this also has Events property.
  • TPyPoint Delphi class implementing a new Python type. It must derive from TPyObject or one of its descendants. Then it must override some methods, like the constructors, the RegisterMethods, and the type services’ virtual methods.
  • 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 Demo21 sample project from the extracted GitHub repository ..Python4DelphiDemosDemo21.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.
  • PythonModule1 with Module name spam is created. Created an eventPythonModule1.Events[0]. And on execute event(PythonModule1Events0Execute) defined with below code.
  • PythonType1 during initialization, associate PyObjectClass with the created TPyPoint class. Created an event PythonType1.Events[0]. And on execute event(PythonType1Events0Execute) defined with below code.
  • Memo1, used for providing the Python Script to execute, and Memo2 for showing the output. 
  • On Clicking Execute Button the python script is executed.
demo21 2959140
<strong>Python4Delphi Demo21<strong>

Python4Delphi makes it easy to build Windows 32 bit and 64 bit apps using Python libraries and a Delphi based UI.

See What's New in 12.2 Athens See What's New in 12.2 Athens Dev Days of Summer 2-24

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