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.
1 2 3 4 5 6 7 8 |
procedure TForm1.PythonModule1Events0Execute(Sender: TObject; PSelf, Args: PPyObject; var Result: PPyObject); begin with GetPythonEngine do begin Result := PyUnicodeFromString('Hello world !'); end; end; |
- 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.
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.PythonType1Events0Execute(Sender: TObject; PSelf, Args: PPyObject; var Result: PPyObject); var dx, dy : Integer; Instance : TPyPoint; begin with GetPythonEngine do begin // Convert the PSelf Python object to a Delphi instance pointer. Instance := TPyPoint(PythonToDelphi(PSelf)); // first we extract the arguments if PyArg_ParseTuple( args, 'ii:Point.Offset',@dx, @dy ) <> 0 then begin // if it's ok, then we call the method that does the job // with the correct arguments Instance.OffsetBy( dx, dy ); // Finally, we return nothing Result := ReturnNone; end else // the arguments were not right Result := nil; end; end; |
- Memo1, used for providing the Python Script to execute, and Memo2 for showing the output.
- On Clicking Execute Button the python script is executed.