Easily Pass Values Between Delphi And Python In Your Windows Delphi/C++ Builder Apps

function TForm1.GetProperty(pSelf, Args : PPyObject) : PPyObject; cdecl;

var

  key : PAnsiChar;

begin

  with GetPythonEngine do

    if PyArg_ParseTuple( args, ‘s:GetProperty’,@key ) > 0 then

      begin

        if key = ‘Title’ then

          Result := VariantAsPyObject(cbTitle.Text)

        else if key = ‘Name’ then

          Result := VariantAsPyObject(edName.Text)

        else if key = ‘Informatician’ then

          Result := VariantAsPyObject(cbInformatician.Checked)

        else if key = ‘PythonUser’ then

          Result := VariantAsPyObject(cbPythonUser.Checked)

        else if key = ‘Age’ then

          Result := VariantAsPyObject(edAge.Text)

        else if key = ‘Sex’ then

          Result := VariantAsPyObject(rgSex.ItemIndex)

        else

          begin

            PyErr_SetString (PyExc_AttributeError^, PAnsiChar(Format(‘Unknown property “%s”‘, [key])));

            Result := nil;

          end;

      end

    else

      Result := nil;

end;

 

function TForm1.SetProperty(pSelf, Args : PPyObject) : PPyObject; cdecl;

var

  key : PAnsiChar;

  value : PPyObject;

begin

  with GetPythonEngine do

    if PyArg_ParseTuple( args, ‘sO:SetProperty’,@key, @value ) > 0 then

      begin

        if key = ‘Title’ then

          begin

            cbTitle.Text := PyObjectAsVariant( value );

            Result := ReturnNone;

          end

        else if key = ‘Name’ then

          begin

            edName.Text := PyObjectAsVariant( value );

            Result := ReturnNone;

          end

        else if key = ‘Informatician’ then

          begin

            cbInformatician.Checked := PyObjectAsVariant( value );

            Result := ReturnNone;

          end

        else if key = ‘PythonUser’ then

          begin

            cbPythonUser.Checked := PyObjectAsVariant( value );

            Result := ReturnNone;

          end

        else if key = ‘Age’ then

          begin

            edAge.Text := PyObjectAsVariant( value );

            Result := ReturnNone;

          end

        else if key = ‘Sex’ then

          begin

            rgSex.ItemIndex := PyObjectAsVariant( value );

            Result := ReturnNone;

          end

        else

          begin

            PyErr_SetString (PyExc_AttributeError^, PAnsiChar(Format(‘Unknown property “%s”‘, [key])));

            Result := nil;

          end;

      end

    else

      Result := nil;

end;