pyscripter / python4delphi

Free components that wrap up Python into Delphi and Lazarus (FPC)
MIT License
906 stars 310 forks source link

How to pass a callback function from Pascal as a parameter to a Python function? #424

Closed heimo88 closed 1 year ago

heimo88 commented 1 year ago

Hi, I have tried several approaches to pass a Delphi callback function to be used by Python, but the functions are not being called. ` procedure CallbackFunc(args: Variant); begin Form1.Memo1.Lines.add('111'); // ... end;

callback:=PythonEngine1.PyCFunction_NewEx(@CallbackFunc, nil,nil); PythonEngine1.Py_INCREF(callback);

//client.subscribe_whole_quote(NewPythonDict,TVarProc(cb)); //codelist := VarPythonCreate(['SZ']); codelist := PythonEngine1.PyList_New(1); PythonEngine1.PyList_SetItem(codelist, 0, PythonEngine1.PyUnicode_FromString('110072.SH'));

argsTuple := PythonEngine1.PyTuple_New(2); PythonEngine1.PyTuple_SetItem(argsTuple, 0, codelist); PythonEngine1.PyTuple_SetItem(argsTuple, 1, callback); ss :=GetPythonEngine.EvalPyFunction(ExtractPythonObjectFrom( client.subscribe_whole_quote ), argsTuple); `

in the code subscribe_whole_quote is a python function have 2 parms, 1st is a list, and 2d is the callback function, when I called the subscribe_whole_quote it returned success, but never call the callback function.

heimo88 commented 1 year ago

the subscribe_whole_quote is in a pyd file. client := Import('pythonclient.pyd');

pyscripter commented 1 year ago

Please use the support forum for questions "how to do this..", "why my program does not work" etc.