Jump to content
IJCdelpi

Passing more than one argument from Python to Delphi

Recommended Posts

Hello,

 

My question is a bit of a novice question because I am kind of new to Delphi and I just started using this library. I am just trying to pass 2 arguments from the python version of a function to the delphi version of the function. I have been using the Demos (demo 3) and the book linked in the readme in the P4D Git repo, but I am a little stuck/confused. I was able to get my really simple Python script to run if I pass 1 argument but if I try to pass 2 arguments to my function in the Python script this is the error I get:

Quote

Traceback (most recent call last):
  File "<string>", line 3, in <module>
TypeError: read_AP_register() takes exactly 1 argument (2 given)

Here are the snippets of code:

Quote

//Delphi Code:

procedure TForm1.PythonModule1Events0Execute(Sender: TObject; PSelf,
  Args: PPyObject; var Result: PPyObject);
Var
  arg1 : LongWord;
  arg2 : LongWord;
  output: LongWord;
begin
  With GetPythonEngine
    Do
      Begin
        if PyArg_ParseTuple(Args, 'i:read_AP_register', @arg1, @arg2)  <> 0
        Then
          begin
            output := read_AP_register(arg1, arg2);
            Result := PyLong_fromLong(output);
          end
        Else
          begin
            Result := Nil;
          end;
      End;
end;


//this is just an nonsense example function
function read_AP_register (arg1, arg2 : LongWord): LongWord;
begin
  result := arg1 - arg2;
end;
//Python Script

from delphi_module import read_AP_register

 

result = read_AP_register(15, 10)

print(result)

 

If anyone has any suggestions or tips that could help out that would be really awesome!

Thanks!
-Ian

Share this post


Link to post
56 minutes ago, IJCdelpi said:

if PyArg_ParseTuple(Args, 'i:read_AP_register', @arg1, @arg2)  <> 0

It should be 'ii:read_AP_register' if you are expecting two integer arguments.

  • Thanks 1

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

×