Jump to content
Celebr0

I/O doesn't work

Recommended Posts

Hello, I ran into a problem that input/output does not work through the OnReceiveUniData/OnReceiveData event and others, but it works through TPythonGUIInputOutput.Output 

 

unit Unit2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, PythonEngine, PythonGUIInputOutput;

type
  TForm2 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure OnReceiveUniData(Sender: TObject; var Data: string);
    procedure CreatePyEngine;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;
  PythonEngine: TPythonEngine;
  working: boolean = True;

implementation

{$R *.dfm}

procedure TForm2.OnReceiveUniData(Sender: TObject; var Data: string);
begin
Memo1.Lines.Add(Data); //No
Memo1.Lines.Add('2'); //No
end;

procedure TForm2.CreatePyEngine;
var
pyio:TPythonGUIInputOutput;
begin
  pyio := TPythonGUIInputOutput.Create(nil);
  pyio.UnicodeIO := True;
  pyio.DelayWrites := True;
  pyio.OnReceiveUniData := OnReceiveUniData;
  //pyio.Output:=Memo1; //------- WORK
  PythonEngine := TPythonEngine.Create(nil);
  PythonEngine.IO := pyio;
  PythonEngine.RedirectIO := True;
  PythonEngine.LoadDll;
  TPythonThread.Py_Begin_Allow_Threads;
end;

procedure TForm2.Button1Click(Sender: TObject);
const Script ='print("Hello")';
begin
CreatePyEngine;
//
TThread.CreateAnonymousThread(
procedure
var
Py: IPyEngineAndGIL;
begin
Py := SafePyEngine;
while working do
PythonEngine.ExecString(Script);
end).Start;
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
working:=False;
end;

end.
 

Share this post


Link to post
  On 8/6/2024 at 11:17 PM, pyscripter said:

TPythonGUIInputOutput works with a linked memo.  Use TPythonInputOutput instead.

Hello, it still doesn't work =(

 

procedure TForm2.CreatePyEngine;
var
pyio:TPythonInputOutput;
begin
  pyio := TPythonInputOutput.Create(nil);
  pyio.UnicodeIO := True;
  pyio.DelayWrites := True;
  pyio.OnReceiveUniData := OnReceiveUniData;
  //pyio.Output:=Memo1; //------- WORK
  PythonEngine := TPythonEngine.Create(nil);
  PythonEngine.IO := pyio;
  PythonEngine.RedirectIO := True;
  PythonEngine.LoadDll;
  TPythonThread.Py_Begin_Allow_Threads;
end;

 

Share this post


Link to post

Use the OnSendUniData event to process data sent by the python engine.

Edited by pyscripter

Share this post


Link to post
  On 8/7/2024 at 12:15 AM, pyscripter said:

Use the OnSendUniData event to process data sent by the python engine.

Too Not work

 

procedure TForm2.OnData(Sender: TObject; const Data: string);
begin
Memo1.Lines.Add(Data); //No
Memo1.Lines.Add('2'); //No
end;

procedure TForm2.CreatePyEngine;
var
pyio:TPythonInputOutput;
begin
  pyio := TPythonInputOutput.Create(nil);
  pyio.UnicodeIO := True;
  pyio.DelayWrites := True;
  pyio.OnSendUniData  := OnData;
  //pyio.Output:=Memo1; //------- WORK
  PythonEngine := TPythonEngine.Create(nil);
  PythonEngine.IO := pyio;
  PythonEngine.RedirectIO := True;
  PythonEngine.LoadDll;
  TPythonThread.Py_Begin_Allow_Threads;
end;

 

Edited by Celebr0

Share this post


Link to post

OnData is called from a thread.  You need to change it to:

 

procedure TForm2.OnData(Sender: TObject; const Data: string);
begin
  TThread.Synchronize(nil,
  procedure
  begin
   Memo1.Lines.Add(Data); //No
    Memo1.Lines.Add('2'); //No
  end);
end;

 

Also set DelayWrites to False.  DelayWrites only makes sense with TPythonGUIInputOutput.

 

With the above two changes your code works as expected.

 

Share this post


Link to post
  On 8/7/2024 at 7:03 PM, pyscripter said:

OnData is called from a thread.  You need to change it to:

Big Thanks This Is Work:

 

procedure TForm2.OnData(Sender: TObject; const Data: string);
begin
Memo1.Lines.Add(Data);
end;

procedure TForm2.CreatePyEngine;
var
pyio:TPythonInputOutput;
begin
  pyio := TPythonInputOutput.Create(nil);
  pyio.UnicodeIO := True;
  pyio.DelayWrites := False;
  pyio.OnSendUniData  := OnData;
  PythonEngine := TPythonEngine.Create(nil);
  PythonEngine.IO := pyio;
  PythonEngine.RedirectIO := True;
  PythonEngine.LoadDll;
  TPythonThread.Py_Begin_Allow_Threads;
end;

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×