我正在使用 Python 3.8 和 Delphi 10.4.2。
我正在尝试使用Python4Delphi的组件通过 Python 脚本访问 Delphi 中定义的一些接口。
在设计时,我将 TPythonEngine、TPythonModule 和 TPyDelphiWrapper 组件添加到我的项目的 VCL 表单中。
所以我定义了3个接口,分别由3个类实现,如下
type
IPerson = interface (IUnknown)
['{1D21B5B6-25DE-4884-8BDB-8E2D9A239D64}']
function GetName : string;
procedure SetName ( value : string );
property Name: string read GetName write SetName;
function GetSurname: string;
procedure SetSurname(value : string);
property Surname : string read GetSurname write SetSurname;
function GetInfo : string;
end;
ICustomer = interface (IPerson)
['{8742364C-33E8-4FF4-86FB-C19AF67A735B}']
function GetCustomerNumber : string;
procedure SetCustomerNumber ( value : string );
property CustomerNumber : string read GetCustomerNumber write SetCustomerNumber;
end;
ISupplier = interface ( IPerson )
['{420FFF78-92DE-4D7E-9958-FDA95748EEB7}']
function GetSupplierNumber : string;
procedure SetSupplierNumber ( value : string );
property SupplierNumber : string read GetSupplierNumber write SetSupplierNumber;
end;
TPerson = class ( TInterfacedObject , IPerson)
private
FName : string;
FSurname : string;
function GetName : string;
procedure SetName ( value : string );
function GetSurname: string;
procedure SetSurname(value : string);
public
property Surname : string read GetSurname write SetSurname;
property Name: string read GetName write SetName;
function GetInfo : string; virtual;
end;
TCustomer = class ( TPerson , ICustomer)
private
FCustomerNumber : string;
function GetCustomerNumber : string;
procedure SetCustomerNumber ( value : string);
public
property CustomerNumber : string read GetCustomerNumber write SetCustomerNumber;
function GetInfo: string; override;
end;
TSupplier = class ( TPerson , ISupplier)
private
FSupplierNumber : string;
function GetSupplierNumber : string;
procedure SetSupplierNumber ( value : string );
public
property SupplierNumber : string read GetSupplierNumber write SetSupplierNumber;
function GetInfo : string; override;
end;
在表单的 Create 方法中,我定义了 3 个变量,为 3 个接口中的每一个定义一个,并通过 PyDelphiWrapper 我将它们传递给 Python 模块中的 3 个不同的 Python 变量。
procedure TFrmTestInterface.FormCreate(Sender: TObject);
var
LPerson : IPerson;
LCustomer : ICustomer;
LSupplier : ISupplier;
Py: PPyObject;
begin
LPerson := TPerson.Create;
LCustomer := TCustomer.Create;
LSupplier := TSupplier.Create;
LPerson.Name := 'Pippo';
LPerson.Surname := 'Rossi';
LCustomer.Name := 'Pluto';
LCustomer.Surname := 'Verdi';
LSupplier.Name := 'Paperino';
LSupplier.Surname := 'Bianchi';
Py := PyDelphiWrapper1.WrapInterface(TValue.From(LPerson));
PythonModule1.SetVar('delphi_person', Py);
GetPythonEngine.Py_DecRef(Py);
Py := PyDelphiWrapper1.WrapInterface(TValue.From(LCustomer));
PythonModule1.SetVar('delphi_customer', Py);
GetPythonEngine.Py_DecRef(Py);
Py := PyDelphiWrapper1.WrapInterface(TValue.From(LSupplier));
PythonModule1.SetVar('delphi_supplier', Py);
GetPythonEngine.Py_DecRef(Py);
end;
在运行时变量被正确解释,但每次我尝试访问接口中定义的属性之一时,我总是得到相同的错误。
这是我尝试运行的 Python 脚本:
from delphi_module import delphi_person, delphi_customer, delphi_supplier
print('type(delphi_person) = ', type(delphi_person))
print('type(delphi_customer) = ', type(delphi_customer))
print('type(delphi_supplier) = ', type(delphi_supplier))
print(delphi_person.Name)
我得到的错误
回溯(最后一次调用):文件“”,第 7 行,在 AttributeError 中:获取属性“名称”时出错。错误:未知属性
type(...) 命令对三个变量正确运行。
如果我不使用接口类型的 3 个变量,而是使用 PyDelphiWrapper.Wrap 方法将每个变量声明为类类型,那么一切正常!
procedure TFrmTestInterface.FormCreate(Sender: TObject);
var
LPerson : IPerson;
LCustomer : ICustomer;
LSupplier : ISupplier;
Py: PPyObject;
begin
LPerson := TPerson.Create;
LCustomer := TCustomer.Create;
LSupplier := TSupplier.Create;
LPerson.Name := 'Pippo';
LPerson.Surname := 'Rossi';
LCustomer.Name := 'Pluto';
LCustomer.Surname := 'Verdi';
LSupplier.Name := 'Paperino';
LSupplier.Surname := 'Grandi';
Py := PyDelphiWrapper1.Wrap(LPerson, TObjectOwnership.soReference);
PythonModule1.SetVar('delphi_person', py);
GetPythonEngine.Py_DECREF(py);
Py := PyDelphiWrapper1.Wrap(LCustomer, TObjectOwnership.soReference);
PythonModule1.SetVar('delphi_customer', py);
GetPythonEngine.Py_DECREF(py);
Py := PyDelphiWrapper1.Wrap(LSupplier, TObjectOwnership.soReference);
PythonModule1.SetVar('delphi_supplier', py);
GetPythonEngine.Py_DECREF(py);
end;
使用相同的 Python 脚本,我得到正确的输出而没有错误
任何人都知道我在使用 TPyDelphiWrapper 为 Python 脚本包装接口类型变量时做错了什么?