Learn How To Dynamically Create And Destroy Python4Delphi Components With A Delphi Windows App
procedure TForm1.CreatePythonComponents;
begin
if cbPyVersions.ItemIndex <0 then begin
ShowMessage(‘No Python version is selected’);
Exit;
end;
// Destroy P4D components
FreeAndNil(PythonEngine1);
FreeAndNil(PythonType1);
FreeAndNil(PythonModule1);
{ TPythonEngine }
PythonEngine1 := TPythonEngine.Create(Self);
PyVersions[cbPyVersions.ItemIndex].AssignTo(PythonEngine1);
PythonEngine1.IO := PythonGUIInputOutput1;
{ TPythonModule }
PythonModule1 := TPythonModule.Create(Self);
PythonModule1.Name := ‘PythonModule1’;
PythonModule1.Engine := PythonEngine1;
PythonModule1.ModuleName := ‘spam’;
with PythonModule1.Errors.Add do begin
Name := ‘PointError’;
ErrorType := etClass;
end;
with PythonModule1.Errors.Add do begin
Name := ‘EBadPoint’;
ErrorType := etClass;
ParentClass.Name := ‘PointError’;
end;
{ TPythonType }
PythonType1 := TPythonType.Create(Self);
PythonType1.Name := ‘PythonType1’;
PythonType1.Engine := PythonEngine1;
PythonType1.OnInitialization := PythonType1Initialization;
PythonType1.TypeName := ‘Point’;
PythonType1.Prefix := ‘Create’;
PythonType1.Services.Basic := [bsRepr,bsStr,bsGetAttrO,bsSetAttrO];
PythonType1.TypeFlags :=
[tpfBaseType];
PythonType1.Module := PythonModule1;
PythonEngine1.LoadDll;
end;