I`m using Python4Delphi
I have a python file that a class declared on it like this :
class Student:
SName = "MyName"
SAge = 26
def GetName(self):
return SName
def GetAge(self):
return SAge
I want to get a refrence of this class and access it`s fields or methods with my Delphi code
I have found an example here : http://www.atug.com/andypatterns/pythonDelphiTalk.htm
But when I try to do it like that example an error shown : "No such Interface supported"
This is my Delphi Code :
var
Err : Boolean;
S : TStringList;
MyClass : OLEVariant;
PObj : PPyObject;
begin
...
S := TStringList.Create;
try
S.LoadFromFile(ClassFileEdit.Text);
Err := False;
try
PyEngine.ExecStrings(S);
except
on E:Exception do
begin
Err := True;
MessageBox(Handle, PChar('Load Error : ' + #13 + E.Message), '', MB_OK+MB_ICONEXCLAMATION);
end;
end;
finally
S.Free;
end;
if Err then
Exit;
Err := False;
try
try
PyEngine.ExecString('ClassVar.Value = Student()');
except
on E:Exception do
begin
Err := True;
MessageBox(Handle, PChar('Class Name Error : ' + #13 + E.Message), '', MB_OK+MB_ICONEXCLAMATION);
end;
end;
finally
if not Err then
begin
PObj := ClassDelphiVar.ValueObject;
MyClass := GetAtom(PObj);
GetPythonEngine.Py_XDECREF(PObj);
NameEdit.Text := MyClass.GetName();
AgeEdit.Text := IntToStr(MyClass.GetAge());
end;
end;
Error occurs on this line :
NameEdit.Text := MyClass.GetName();
It seems that MyClass not filled with Student Object
I searched a lot and I found that GetAtom is deprecated in new versions, but how I can do this in another way ?
- ClassDelphiVar is a TPythonDelphiVar component with "ClassVar" as VarName