Jump to content

Passing event data to Delphi from an event in a Module

Recommended Posts

Posted (edited)

Some sample python...

import adelphimodule

class ADict(dict):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.__dict__ = self

someitems = ADict(
	first = 1,
	second = "Two",
	third = 3.0,
	fourth = ADict(
		subitem1 = True


Now, in Delphi I stick a PythonModule on a form and hook it up to engine etc (i.e. all this bit works) - I name it delphimodule which will then make it accessible with the Python above.

Next I create an event for the module called delphievent

In delphi I run the Python and when it hits the adelphimodule.delphievent(someitems) which then triggers this bit of Delphi...


procedure TForm1.PythonModule1Events0Execute(Sender: TObject; PSelf, Args: PPyObject;
  var Result: PPyObject);
  log: TTrainLog;
  with GetPythonEngine do
    // Check if the transmitted object is a dictionary
    if not PyDict_Check(Args) then
      Result := PyUnicodeFromString('===> Bad Event Handler!');
  Result := PyUnicodeFromString('===> Good Event Handler!');


This code will make the Python print "Bad Event Handler" even though I've passed the event data that is a Python dict


The examples have a single event demo and it does nothing with passing data so I'm a little stumped over how to extract the data I passed in


I could write yet another module I guess but then I'd end up needing new modules for any new bunch of data I sent back to Delphi. The thing I'm trying to do is pass arbitary data back to delphi (tried packaging it as JSon but couldn't read ther string data either) 


Edit : Got string passing OK now so can always pass as JSON


Edited by peardox

Share this post

Link to post

Args is a tuple not a dictionary.  Search in the P4D source code for  PyArg_ParseTuple to see how to make use of Args.  There are hundreds of examples!

Share this post

Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now