Celebr0 0 Posted August 6 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
pyscripter 694 Posted August 6 TPythonGUIInputOutput works with a linked memo. Use TPythonInputOutput instead. Share this post Link to post
Celebr0 0 Posted August 6 20 minutes ago, 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
pyscripter 694 Posted August 7 (edited) Use the OnSendUniData event to process data sent by the python engine. Edited August 7 by pyscripter Share this post Link to post
Celebr0 0 Posted August 7 (edited) 40 minutes ago, 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 August 7 by Celebr0 Share this post Link to post
pyscripter 694 Posted August 7 Can you post a zip file with your project? Share this post Link to post
Celebr0 0 Posted August 7 4 hours ago, pyscripter said: Can you post a zip file with your project? Hi of course I can py4delph.zip Share this post Link to post
pyscripter 694 Posted August 7 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
Celebr0 0 Posted August 7 35 minutes ago, 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