pyscripter / python4delphi

Free components that wrap up Python into Delphi and Lazarus (FPC)
MIT License
895 stars 307 forks source link

How can I output the results of a py script? #462

Closed Uefi1 closed 3 months ago

Uefi1 commented 3 months ago

Hello, how can I output the results to a string variable of a .py script running in a multi-threaded application ?

function ExecPythonFile(const Filename):string;
var
FGILState: PyGILstate_STATE;
begin
Result := '';
FGILState := PythonEngine.PyGILState_Ensure;
try
PythonEngine.ExecFile(Filename);
except
PythonEngine.IO.Write(Result); //No WORK ((((
end;
PythonEngine.PyGILState_Release(FGILState);
end;
Alexey-T commented 3 months ago

Exec the script containing 'print( )' and use

property IO: TPythonInputOutput read FIO write FIO;               
property RedirectIO: Boolean read FRedirectIO write FRedirectIO default True;   
Uefi1 commented 3 months ago

Exec the script containing 'print( )' and use

Thanks, I figured it out a little but it doesn't work (

function ExecPythonFile(const Filename):string;
var
FGILState: PyGILstate_STATE;
PythonInputOutput:TPythonGUIInputOutput;
begin
Result := '';
PythonInputOutput:=TPythonGUIInputOutput.Create(nil);
PythonEngine.IO:=PythonInputOutput;
Result:=PythonInputOutput.Output.Text;
FGILState := PythonEngine.PyGILState_Ensure;
try
PythonEngine.DoRedirectIO;
PythonEngine.ExecFile(Filename);
except
end;
PythonEngine.PyGILState_Release(FGILState);
PythonInputOutput.Free;
end;
Alexey-T commented 3 months ago

Result:=PythonInputOutput.Output.Text; PythonEngine.DoRedirectIO

I never used these 2 methods. what I use:

Screenshot from 2024-03-11 13-24-58

Uefi1 commented 3 months ago

I never used these 2 methods.

Sorry I don't understand your code a bit (

Alexey-T commented 3 months ago
Uefi1 commented 3 months ago
  • I attach PythonIO to TPythonEngine.IO property

Still, I think that my last option should work, but I don’t understand why it doesn’t work ( Thank you very much for your help, of course, but you have some other language in which you write, so I don’t understand it yet

pyscripter commented 3 months ago
Uefi1 commented 3 months ago
  • Use support forum for answers to questions, not the Issue tracker. If you do it again I will blacklist you from this repo.

Hello, I think I did everything as you said, the output still doesn’t work (

var
PythonEngine: TPythonEngine;
PythonInputOutput:TPythonGUIInputOutput;

procedure InitPython;
begin
  PythonEngine := TPythonEngine.Create(nil);
  try
    PythonInputOutput:=TPythonGUIInputOutput.Create(nil);
    PythonInputOutput.DelayWrites:=True;
    PythonEngine.InitThreads := True;
    PythonEngine.UseLastKnownVersion := True;
    PythonEngine.AutoLoad := False;
    PythonEngine.AutoFinalize := False;
    PythonEngine.AutoUnload := False;
    PythonEngine.RedirectIO := True;
    PythonEngine.IO:=PythonInputOutput;
    PythonEngine.LoadDll;
    TPythonThread.Py_Begin_Allow_Threads;
  finally
  end;
end;

function ExecPythonFile(const Filename:string):string;
var
FGILState: PyGILstate_STATE;
begin
Result := '';
FGILState := PythonEngine.PyGILState_Ensure;
try
PythonEngine.ExecFile(Filename);
except
end;
PythonEngine.PyGILState_Release(FGILState);
Result:=PythonInputOutput.Output.Text;
end;

Once again, the Issue tracker on GitHub exists for questions, ideas, feedback, tasks, or bugs for work on GitHub.: https://docs.github.com/en/issues/tracking-your-work-with-issues/about-issues

Why the hell should I register on forums with dubious content for each individual repository on Github, and what if the database is stolen on such a forum?

Uefi1 commented 3 months ago

In general, the Python4Delphi BUG does not want to display data for any parameters or settings or assigned events onsenddata onreceivedata